# Relational tables - dependent =\> :update?

**URL:** <https://www.ruby-forum.com/t/relational-tables-dependent-update/51244>\
**Category:** Rails\
**Created:** [November 30, 2005, 8:44pm UTC](https://www.ruby-forum.com/t/relational-tables-dependent-update/51244 "2005-11-30T20:44:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![n1c](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3ec8ea/32.png) [@n1c](https://www.ruby-forum.com/u/n1c)\
**Post date:** [November 30, 2005, 8:44pm UTC](https://www.ruby-forum.com/t/relational-tables-dependent-update/51244/1 "2005-11-30T20:44:03Z")

</div>

I have two classes ‘Company’ and ‘CompanyType’

Company has\_one :company\_type and  
CompanyType belongs\_to :company

My company\_type table contains a field called “ctype” - when I modify  
this, I’d like all companies of that type to update to the new modified  
name.

Is there a way to do this with dependencies, or would anyone be kind  
enough to suggest another way?

---

<div class="post-metadata">

**Author:** ![n1c](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3ec8ea/32.png) [@n1c](https://www.ruby-forum.com/u/n1c)\
**Post date:** [December 1, 2005, 4:41pm UTC](https://www.ruby-forum.com/t/relational-tables-dependent-update/51244/2 "2005-12-01T16:41:51Z")

</div>

I think you have the relationships reversed. If a company can be one of  
many  
different types, your relationships should be:

Company  
belongs\_to :company\_type, :foreign\_key =\> ‘ctype\_id’

CompanyType  
has\_many :companies

and your tables should be:

Company  
id  
…  
ctype\_id

Company\_Types  
id  
ctype  
…

Now you may change the value for ctype in the Company Types table and  
the  
changes will be reflected in all the companies that reference that  
company  
type. eg

mycompany.ctype.ctype

Hammed

---

<div class="post-metadata">

**Author:** ![n1c](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3ec8ea/32.png) [@n1c](https://www.ruby-forum.com/u/n1c)\
**Post date:** [December 1, 2005, 5:01pm UTC](https://www.ruby-forum.com/t/relational-tables-dependent-update/51244/3 "2005-12-01T17:01:19Z")

</div>

hammed wrote:

> I think you have the relationships reversed.

You were right! Absolutely fantastic - thanks ever so much for your  
help. I’m still getting to grips with RoR and table relationships.

Many thanks again.
