What inverse_of
does mean in mongoid associations? What I can get by using it instead of just association without it?

- 14,592
- 6
- 54
- 77
1 Answers
In a simple relation, two models can only be related in a single way, and the name of the relation is automatically the name of the model it is related to. This is fine in most cases, but isn't always enough.
inverse_of
allows you to specify the relation you are referring to. This is helpful in cases where you want to use custom names for your relations. For example:
class User
include Mongoid::Document
has_many :requests, class_name: "Request", inverse_of: :requester
has_many :assignments, class_name: "Request", inverse_of: :worker
end
class Request
include Mongoid::Document
belongs_to :requester, class_name: "User", inverse_of: :requests
belongs_to :worker, class_name: "User", inverse_of: :assignments
end
In this example, users can both request and be assigned to tickets. In order to represent these two distinct relationships, we need to define two relations to the same model but with different names. Using inverse_of
lets Mongoid know that "requests" goes with "requester" and "assignments" goes with "worker." The advantage here is twofold, we get to use meaningful names for our relation, and we can have two models related in multiple ways. Check out the Mongoid Relations documentation for more detailed information.

- 726
- 9
- 20
-
2So I don't need ```inverse of``` if there is only one relation with custom name but I need in case of two relations with custom names? I now that in ActiveRecord ```inverse_of``` do different thing. – freemanoid May 31 '13 at 15:29
-
2Yes, for a single relation with a custom name, you need only use `class_name` (in both Active Record and Mongoid). If `users` and `requests` are related in more than one way, `inverse_of` is the only way Mongoid (and Active Record) could know which one you're referring to. – XanderStrike May 31 '13 at 16:25