Gabriele Tassoni
2009-Mar-04  11:26 UTC
Which are the right :joins for two models in which one has two belongs_to the other same model?
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.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---