Hello,
How can one get the extra attributes from the association table while
using lazy loading like so:
I have a HMT (rails3) association and am using the includes method
to prepare my queries. But Im not able to get at the extra fields in
the association table this way.
Schema
create_table “consultants_projects”, :force => true do |t|
t.integer “consultant_id”
t.integer “project_id”
t.integer “pay_rate”
t.integer “bill_rate”
t.date “start_date”
t.date “end_date”
t.boolean “enabled”, :default => true
end
add_index “consultants_projects”, [“consultant_id”], :name =>
“index_consultants_projects_on_consultant_id”
add_index “consultants_projects”, [“project_id”], :name =>
“index_consultants_projects_on_project_id”
Model
class Project < ActiveRecord::Base
has_many :managers_projects
has_many :managers, :through => :managers_projects
We want all the extra fields on the consultants_projects
association
has_many :consultants_projects
has_many :consultants,
:through => :consultants_projects,
:select => ‘consultants.*, consultants.id,’+
'consultants_projects.id as consultants_projects_id, ’
+
‘consultants_projects.project_id,’ +
‘consultants_projects.pay_rate,’ +
‘consultants_projects.pay_rate,’ +
‘consultants_projects.bill_rate,’ +
‘consultants_projects.start_date,’ +
‘consultants_projects.end_date,’ +
‘consultants_projects.enabled AS
consultants_projects_enabled’
end
c = Consultant.includes(:projects)
c.first.projects
Does not include the extra fields like pay_rate, bill_rate etc that
are stored in the association table.