binarylogic / searchlogic

Searchlogic provides object based searching, common named scopes, and other useful tools.
http://rdoc.info/projects/binarylogic/searchlogic
MIT License
1.39k stars 133 forks source link

items_id_equals_any returns duplicate records #102

Open samdk opened 14 years ago

samdk commented 14 years ago

The database for my application is set up with a many-to-many association between two models: Users and Groups (using a joining class).

I'm trying to do a search of the form:

User.groups_id_equals_any(list_of_group_ids)

That's returning duplicate records when a user is a part of multiple groups in the list_of_group_ids. So if u1/u2/u3 are Users something like this will happen:

>> u1.groups.collect {|g| g.id}.sort
=> [1, 2, 3]
>> u2.groups.collect {|g| g.id}.sort
=> [1, 3]
>> u3.groups.collect {|g| g.id}.sort
=> [1]
>> User.groups_id_equals_any([1,2,3]).size
=> 6 # 3 copies of u1, 2 of u2, 1 of u3
>> User.groups_id_equals_any([1,2,3]).uniq.size
=> 3 # 1 copy each of u1, u2, u3