Rails 3 associations extra attributes with lazy loading


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, '

More observations I've found. The Arel includes method does not seem to respect HMT :select joins. I'd prefer to use the "includes" lazy- laoding way, but cannot seem to get the extra attributes to come back.

Both will return the Manager Model

interest topic. in console, use to_sql to represent SQL, Maybe found more hits.

hi,Will can you use gist to provide more code? it is examine your code.


Sure, here is the schema, and model files. Let me know what you think. Why can't I get the :select to work when using ".includes". In the console examples below, you can see the pay_rate and bill_rate are not present in the example using .includes.

Schema https://gist.github.com/76e70c7f31b6dcc5cfcc

Consultant Model https://gist.github.com/93e5858417bb14ec18c9

Project Model https://gist.github.com/8545e9da49742b4e4dcb

Console output using .includes does not honor :select

I've fixed my issues. Should have been using joins vs includes as there was really no joins happening with eager loading.

This’s what my think. use sql log you can see more details.