rails/rails — Issue #4016: Duplicate joins created when using has_many :through and explicit joins
Author: dvandersluis
Created: 2011-12-18 06:10:37+00:00
State: open

I'm not sure if this is related to #2556, but I'm using Rails 3.1.3 and experiencing this problem, so if it is, then it's not resolved still.

class Client < ActiveRecord::Base
has_many :jobs
has_many :job_applications, :through => :jobs
end

class JobApplications
belongs_to :job
end

irb(main):001:0> Client.find(10).job_applications.joins(:job)
ActiveRecord::StatementInvalid: Mysql2::Error: Not unique table/alias: 'jobs': SELECT `job_applications`.* FROM `job_applications` INNER JOIN `jobs` ON `jobs`.`id` = `job_applications`.`job_id` INNER JOIN `jobs` ON `job_applications`.`job_id` = `jobs`.`id` WHERE `jobs`.`client_id` = 10

As you can see, ```INNER JOIN `jobs` ON `jobs`.`id` = `job_applications`.`job_id` ``` is getting duplicated.