views:

127

answers:

2

If Parent has_many :children, must Child :belongs_to :parent?

+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
+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
+1 That link was really helpful, thanks!
DJTripleThreat