Description
Hi all!
Let's say I have a model such as:
class Recommendation < ActiveRecord::Base
belongs_to :user
belongs_to :target_user, class_name: 'User'
end
Now, say my Ransack search filters looks like this (as JSON, as I'm using Ransack with a REST API. Hopefully, it's more readable):
q: {
user: {
job_title: {
eq: "Designer"
}
},
target_user: {
group: {
name: {
eq: "Design"
}
}
}
}
When I was on Rails 4.0.2 and Ransack 1.2.1, this was the SQL output I received:
"SELECT \"recommendations\".* FROM \"recommendations\"
LEFT OUTER JOIN \"users\" ON \"users\".\"id\" = \"recommendations\".\"user_id\"
LEFT OUTER JOIN \"users\" \"target_users_recommendations\" ON \"target_users_recommendations\".\"id\" = \"recommendations\".\"target_user_id\"
LEFT OUTER JOIN \"groups\" ON \"groups\".\"id\" = \"target_users_recommendations\".\"department_id\"
WHERE ((\"users\".\"job_title\" = 'Designer' AND \"groups\".\"name\" = 'Design'))"
Notice how the group is joined here (4th line, last LEFT OUTER JOIN).
However, after upgrading to Rails 4.1.1 and the Ransack 4.1 branch, this is the SQL output, which is wrong, as notice now how the group is joined (it's joined on the first user association, not the target user association).
"SELECT \"recommendations\".* FROM \"recommendations\"
LEFT OUTER JOIN \"users\" ON \"users\".\"id\" = \"recommendations\".\"user_id\"
LEFT OUTER JOIN \"users\" \"target_users_recommendations\" ON \"target_users_recommendations\".\"id\" = \"recommendations\".\"target_user_id\"
LEFT OUTER JOIN \"groups\" ON \"groups\".\"id\" = \"users\".\"department_id\"
WHERE ((\"users\".\"job_title\" = 'Designer' AND \"groups\".\"name\" = 'Design'))"
This is consistent across several specs I have failing at the moment. Any query on an association for the second user association will fail, as it joins on the former user association.
Hopefully this is enough to get you guys started, but if not, please let me know. I'm going to try and find some time to dig deeper myself. Thanks!