jnunemaker/mongomapper — comment on commit e755e1aab0257e575e46777ceab13ea4619cf905
File:  line 0
By digitalplaywright:

We have a web-app that we'd like to transition Mongoid to MongoMapper that use their inverse_of syntax. I'd like to categorize how we use their inverse_of' feature in case that helps. I believe our use-cases fits well with the 'inverse_of' syntax @bkeepers suggests. Number 2 is the use case that may not fit with the syntax without some extra work. I've suggested a syntax for number 2 within the embedded example.

I've attempted to translate the way we currently use inverse_of in Mongoid to the @bkeepers inverse_of syntax.

We use inverse_of in three ways:

1) Inverse_of is used to declare multiple fields using the same embedded model, like e.g:

```
class Profile
include MongoMapper::Document

many :i_am_a, :inverse_of => :tag_interactions
many :i_love_to, :inverse_of => :tag_interactions
end

class TagInteraction
include MongoMapper::EmbeddedDocument

end
```
This could apply to anything that has different uses of the same concept, like e.g labels and reviews.

2) Inverse_of is used to declare multiple associations to the same model. If the user want two-sided relations like this I suggest extending @bkeepers idea to require declaring an inverse_of association with the same name on both sides so that :from can be inferred, like e.g:

```
class Profile
include MongoMapper::Document

many :admins, :inverse_of => :events
many :attends, :inverse_of => :events
end

class Event
include MongoMapper::Document

#In Mongoid we specify here that:
# Event::admins is the inverse of Profile::administrates
# Event::attendees is the inverse of Profile::attends

many :admins, :inverse_of => :profiles
many :attends, :inverse_of => :profiles
end
```

The array storing the variables could be "#{self.model_name}_#{association_field_name}_ids".

3) Inverse_of is used to change the name of an association to something more informative in the context, like e.g:

```
class Profile
include MongoMapper::Document

many :reviews_of_me, :inverse_of => :reviews
end

class Review
include MongoMapper::Document

end
```