-
Type: New Feature
-
Resolution: Unresolved
-
Priority: Major - P3
-
None
-
Affects Version/s: None
-
Component/s: Associations
-
None
I'm not sure if it's a bug or intentional but there are complications around querying has_and_belongs_to_many relationships that are not covered here and really tripped me up
given a Task with the following
has_and_belongs_to_many :assignees, class_name: "Person", inverse_of: :tasks
and a Person that
has_and_belongs_to_many :tasks, class_name: "Task", inverse_of: :assignees
Expected Behavior
as a user i expect to be able to query for tasks that have people as their assignees using Person objects with the following
Task.in(assignees: [my_person_object]).count
I expect this because when there is a belongs_to relationship i can say things like this without problem
PullRequest.in(person: [my_person_object]).count
Actual Behavior
Task.in(assignees: [my_person_object]).count
but that produces the following error
Value does not define its BSON serialized type: #<Person:0x00000001142b04c0> (BSON::Error::UnserializableClass)
The only way I've found to make it work is to do the following:
Task.in(:assignee_ids.in => [my_person_object.id]).count
Comments
I don't believe that it is correct that some relationships you can make an .in query with an array of Mongoid::Document objects, but in some you have to know the underlying fields that mongoid creates (in this case assignee_ids) and that you have to query using ids.
I believe .in queries should behave consistently and support the same types of objects in the array passed to it regardless of the relationship type it's querying on (presuming it makes any sense to use .in in the first place)