Which are the right :joins for two models in which one has two belongs_to the other same model?


#1

Hi all,
here is the main picture (obviously some snips are made):

class JobFile < ActiveRecord::Base
belongs_to :car
belongs_to :counterpart_insurance, :class_name =>
‘Insurance’, :foreign_key => ‘counterpart_insurance_id’
belongs_to :customer_insurance, :class_name =>
‘Insurance’, :foreign_key => ‘customer_insurance_id’
end

class Insurance < ActiveRecord::Base
has_many :accident_counterparts, :class_name =>
‘JobFile’, :foreign_key => 'customer_insurance_id"
has_many :accident_customers, :class_name => ‘JobFile’, :foreign_key
=> ‘counterpart_insurance_id’
end

class Customer < ActiveRecord::Base
has_many :cars, :dependent => :destroy
end

class Car < ActiveRecord::Base
belongs_to :customer
has_many :job_files, :dependent => :destroy
end

Now, I would like to have all the customers that have a job_file that
deals with an insurance, I’m a bit stuck at writing down the joins
clause for the named scope:

named_scope :insurances_in, lambda { |array| {
:joins => [{:cars => {:job_files => :counterpart_insurance}},
{:cars => {:job_files => :counterpart_insurance}}],
:conditions => “insurances.name LIKE '%#{array.join(”%")}%’",
:group => “customers.id” }
}

Is this right?
Thanks for the help
G.