The docs say you can use inverse_of: nil but don't really describe a use case: http://mongoid.org/en/mongoid/docs/relations.html#has_and_belongs_to_many
I assume this is useful when one object has a LOT of the other, so you can just skip this side completely with inverse_of nil and save some storage space correctly?
So for example:
class Post has_and_belongs_to_many :tags end class Tag has_and_belongs_to_many :posts, inverse_of: nil end
A tag can belong to hundreds or thousands of posts, but a post probably has only 5 tags or so.
So is this a good option for this? I guess you can still do
tag.posts
etc., as usual, and the main compromise is that it changes the request:
Post.find(tag.post_ids)
in
Post.where(tag_ids: tag.id)
If you have a pointer to tag_ids, it looks like it will still be pretty fast. So perhaps the best thing is something like:
class Post has_and_belongs_to_many :tags, index: true end class Tag has_and_belongs_to_many :posts, inverse_of: nil end
I just want to test my thoughts.
Brian armstrong
source share