I have these models: Company; Categories; Classifications. A Company has many categories and a Category has many companies. A Company for each of its own categories has a Classification. For example: Company-1 has Category-1 and Category-2. Company-1 - Category-1 has Classification-III while Company-1 - Category-2 has Classification IV. How can I declare associations? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Wed, Dec 14, 2011 at 11:12 PM, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have these models: > > Company; > Categories; > Classifications. > > A Company has many categories and a Category has many companies. > A Company for each of its own categories has a Classification. > For example: > Company-1 has Category-1 and Category-2. > Company-1 - Category-1 has Classification-III while Company-1 - > Category-2 has Classification IV. > How can I declare associations? >Relevant documentation is here: http://guides.rubyonrails.org/association_basics.html (For similar results, google for "Rails Guides <term on interest>"). If I understand correctly, your case would be a perfect case for has_many :through (and _not_ habtm) because you need Classification attributes on each association between a Company and a Category (then the Classification table is the association table between Company and Category). Try to work out the code for your case from that documentation. If you have difficulties with it, please come back with your specific problems, showing us the code your wrote. HTH, Peter -- http://twitter.com/peter_v http://rails.vandenabeele.com -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 14 December 2011 23:47, Peter Vandenabeele <peter-jNuWw7i2w7syMbTcgqFhxg@public.gmane.org> wrote:> On Wed, Dec 14, 2011 at 11:12 PM, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >> I have these models: >> >> Company; >> Categories; >> Classifications. >> >> A Company has many categories and a Category has many companies. >> A Company for each of its own categories has a Classification. >> For example: >> Company-1 has Category-1 and Category-2. >> Company-1 - Category-1 has Classification-III while Company-1 - >> Category-2 has Classification IV. >> How can I declare associations? > > > Relevant documentation is here: > > http://guides.rubyonrails.org/association_basics.html > > (For similar results, google for "Rails Guides <term on interest>"). > > If I understand correctly, your case would be a perfect case for > has_many :through (and _not_ habtm) because you need > Classification attributes on each association between a > Company and a Category (then the Classification table is > the association table between Company and Category).I''m also was thinking about has_many :through. The join model is Classification with attriìbutes like: company_id category_id classification_type classification_amount. But if classification.amount for classification.type II change I must change all the occurrences for company and categories where classification.type II occurs. I think its better to use a join table like this: company_id category_id classification_id What do you think about? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Thu, Dec 15, 2011 at 12:20 AM, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 14 December 2011 23:47, Peter Vandenabeele <peter-jNuWw7i2w7syMbTcgqFhxg@public.gmane.org> > wrote: > > On Wed, Dec 14, 2011 at 11:12 PM, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> > >> I have these models: > >> > >> Company; > >> Categories; > >> Classifications. > >> > >> A Company has many categories and a Category has many companies. > >> A Company for each of its own categories has a Classification. > >> For example: > >> Company-1 has Category-1 and Category-2. > >> Company-1 - Category-1 has Classification-III while Company-1 - > >> Category-2 has Classification IV. > >> How can I declare associations? > > > > > > Relevant documentation is here: > > > > http://guides.rubyonrails.org/association_basics.html > > > > (For similar results, google for "Rails Guides <term on interest>"). > > > > If I understand correctly, your case would be a perfect case for > > has_many :through (and _not_ habtm) because you need > > Classification attributes on each association between a > > Company and a Category (then the Classification table is > > the association table between Company and Category). > > I''m also was thinking about has_many :through. > The join model is Classification with attriìbutes like: > company_id > category_id > classification_type > classification_amount. > > But if classification.amount for classification.type II change I must > change all the occurrences for company and categories where > classification.type II occurs. > > I think its better to use a join table like this: > company_id > category_id > classification_id > > What do you think about? >Indeed. I had not understood there are only a limited number of Classification types possible between the Company and the Category. In that case, indeed it is better that the association table does not have the Classification attributes itself, but you normalized out these attributes to a third Classification table (with the Classification#classification_type Classification.amount attributes). WARNING: do not use ''type'' as a column name, it is a special column that is used for STI (Single Table Inheritance). Unless you would really want to use STI on your Classifications (which might make sense in an OO design if certain Classification types have special functionality that is different from other Classification types). HTH, Peter -- http://rails.vandenabeele.com http://twitter.com/peter_v -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.