If Parent has_many :children
, must Child :belongs_to :parent
?
No, the belongs_to
isn't necessary. It will mean that you won't be able to query the association from both directions, but if that's not a requirement for your app, then it's not necessary.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With