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
====================================
ruby-1.8.7-p249 > p = Project.first.managers.first
 => #<Manager id: 1, first_name: "Edward", last_name: "Scissorhands",
email: nil, phone: nil, created_at: "2010-04-22 22:52:29", updated_at:
"2010-04-22 22:52:29">

ruby-1.8.7-p249 > p = Project.includes(:managers).first.managers.first
 => #<Manager id: 1, first_name: "Edward", last_name: "Scissorhands",
email: nil, phone: nil, created_at: "2010-04-22 22:52:29", updated_at:
"2010-04-22 22:52:29">


Notice that all the extra attributes are NOT passed
===================================
ruby-1.8.7-p249 > p =
Project.includes(:managers).first.managers.first.to_json
 => "{\"created_at\":\"2010-04-22T22:52:29Z\",\"updated_at\":
\"2010-04-22T22:52:29Z\",\"id\":1,\"phone\":null,\"last_name\":
\"Scissorhands\",\"first_name\":\"Edward\",\"email\":null}"


Notice that all the extra attributes are passed
===================================
ruby-1.8.7-p249 > p = Project.first.managers.first.to_json
 => "{\"created_at\":\"2010-04-22T22:52:29Z\",\"updated_at\":
\"2010-04-22T22:52:29Z\",\"project_id\":\"1\",\"primary\":\"t\",
\"manager_id\":\"1\",\"id\":1,\"phone\":null,\"last_name\":
\"Scissorhands\",\"first_name\":\"Edward\",\"email\":null}"


Here is the setup in my Model
===================================
has_many :managers_projects
has_many :managers, :through => :managers_projects,
  :select => 'managers.*, managers_projects.project_id,
managers_projects.manager_id, managers_projects.`primary`'



On Apr 21, 5:00 pm, Will Prater <[email protected]> wrote:
> 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.
>
> --
> 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 [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group 
> athttp://groups.google.com/group/rubyonrails-talk?hl=en.

-- 
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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to