Does has_many have a requirement to belong_ on the other hand?

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

+7
ruby-on-rails associations
source share
2 answers

No, the belongs_to property is optional. This will mean that you will not be able to request a connection from both directions, but if this is not a requirement for your application, then this is not necessary.

+15
source share

According to Cody, not necessarily - but it will be a 90% case.

See the Rails API Doc for ActiveRecord Associations . When you specify either has_many or belongs_to , many (helper) methods are added to the Rails Model class. The page above shows specific methods that are automatically added in tabular format. Therefore, if you simply indicate one end of the association, for example. Child, you can only use these methods from the Child model.

+11
source share

All Articles