Forum: Ruby on Rails Relational tables - dependent => :update ?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Nick C. (Guest)
on 2005-11-30 21:44
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?
hammed (Guest)
on 2005-12-01 17:41
(Received via mailing list)
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
n1C (Guest)
on 2005-12-01 18:01
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.
This topic is locked and can not be replied to.