Alex
Alex

Reputation: 6475

Active Record joins over 3 tables

I am trying to find all terms and courses that apply to a contact.

Here are my models

class Register < ActiveRecord::Base
 belongs_to :session
 belongs_to :contact
end

class Session < ActiveRecord::Base
 belongs_to :term  
 belongs_to :course
 has_many :registers
 has_many :contacts, :through => :registers
end

Here is the find a wrote

@data = Register.all :joins => {:session =>[:term, :course]}  , :conditions => ["contact_id = ?", params[:id]]

When I run the query all I get is the session records not the terms or courses

Thanks

Alex

Upvotes: 2

Views: 616

Answers (1)

Slobodan Kovacevic
Slobodan Kovacevic

Reputation: 6888

Try using :include instead of :joins. Something like:

@data = Register.all :include => {:session =>[:term, :course]}  , :conditions => ["contact_id = ?", params[:id]]

Upvotes: 4

Related Questions