belongs_to vs belongs_to_related, has_many vs has_

2019-07-21 07:18发布

问题:

What does the _related add to a relation? For example what is the difference between belongs_to and belongs_to_related? I'm mostly seeing this in Mongoid apps but not sure if it applies to Rails in general as well.

回答1:

belongs_to_related represents a relational association to a “parent” object.

please check this issue and the example given here on GitHub:

https://github.com/mongoid/mongoid/issues/348

See also:

http://rubydoc.info/gems/mongoid-with-auth/1.9.4/Mongoid/Associations/BelongsToRelated


has_many_related represents a relational one-to-many association with an object in a separate collection or database.

See also:

http://rubydoc.info/gems/mongoid-rails2/1.9.4/Mongoid/Associations/HasManyRelated

http://groups.google.com/group/mongoid/browse_thread/thread/29133cb1691e7db2/7d973df673861264