If Parent has_many :children
, must Child :belongs_to :parent
?
views:
127answers:
2
+8
A:
No, the belongs_to isnt necessary. It will mean that you wont be able to query the association from both directions, but if that is not a requirement for your app, then its not necessary.
Cody Caughlan
2009-05-28 16:09:22
+4
A:
As Cody says, not mandatory - but will be the 90% case.
See the Rails API Doc for ActiveRecord Associations.
When you specify either has_many
or belongs_to
, a bunch of (helper) methods get added to your Rails Model class. The page above shows the specific methods that get added automatically in tabular format. So if you just specify one end of the association e.g. the Child, you'd only be able to use those corresponding methods from the Child model.
Gishu
2009-05-28 16:16:11
+1 That link was really helpful, thanks!
DJTripleThreat
2010-05-01 10:11:41