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

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Gabriele T. (Guest)
on 2009-03-04 13:27
(Received via mailing list)
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'

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'

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

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

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 => " LIKE '%#{array.join("%")}%'",
    :group => "" }

Is this right?
Thanks for the help
This topic is locked and can not be replied to.