Steps to reproduce the issue:
-
Clone this Gist in two environments, one running Rails 4.0, one running Rails 4.1/4.2.
-
Run
bundle install
andrake db:migrate
(don't forget to change the Rails version in the Gemfile); -
Open the rails console:
rails c
- Create some test data. Here's an example:
p1 = Party.create(name: 'Party 1')
p2 = Party.create(name: 'Party 2')
mc = MasterClient.create(party: p1)
c = Client.create(party: p2, master_client: mc)
- Issue a ransack search:
Client.ransack('master_client_party_name_cont' => 'arty').result
- Observe the generated query:
a. On Rails 4.0, the query looks as expected:
SELECT "clients".*
FROM "clients"
LEFT OUTER JOIN "clients" "master_clients_clients" ON "master_clients_clients"."id" = "clients"."master_client_id" AND "master_clients_clients"."type" IN ('MasterClient')
LEFT OUTER JOIN "parties" ON "parties"."id" = "master_clients_clients"."party_id"
WHERE ("parties"."name" LIKE '%arty%')
b. On Rails 4.1/4.2, the last join is built the wrong way:
SELECT "clients".*
FROM "clients"
LEFT OUTER JOIN "clients" "master_clients_clients" ON "master_clients_clients"."id" = "clients"."master_client_id" AND "master_clients_clients"."type" IN ('MasterClient')
LEFT OUTER JOIN "parties" ON "parties"."id" = "clients"."party_id"
WHERE ("parties"."name" LIKE '%arty%')