Open
Description
I wrote this failing spec:
it 'evaluates single condition for multiple `belongs_to` associations to the same table' do
# Why Search.new(Recommendation.joins(:person, :target_person)...) is not the same thing?
s = Recommendation.joins(:person, :target_person).ransack(
{ target_person_name_eq: 'Test' },
).result
expect(s).to be_an ActiveRecord::Relation
real_query = remove_quotes_and_backticks(s.to_sql)
expected_query = <<-SQL
SELECT recommendations.* FROM recommendations
INNER JOIN people ON people.id = recommendations.person_id
INNER JOIN people target_people_recommendations
ON target_people_recommendations.id = recommendations.target_person_id
WHERE target_people_recommendations.name = 'Test'
SQL
.squish
expect(real_query).to eq expected_query
end
It results in:
WHERE people.name = 'Test'"
Instead of:
WHERE target_people_recommendations.name = 'Test'"
And it only happens if there isn't a filter on the other table, If I write what the existing spec is testing:
s = Recommendation.joins(:person, :target_person).ransack(
{ person_name_eq: 'Ernie', target_person_name_eq: 'Test' },
).result
Then it works as expected:
WHERE (people.name = 'Ernie' AND target_people_recommendations.name = 'Test')"
This looks like a bug to me, I have one query that I need to join the same table twice, and this is getting the wrong results. I'm joining both tables because I'm selecting specific columns from both tables, but I need to filter by the aliased one.
Metadata
Metadata
Assignees
Labels
No labels