# Rails 3 associations extra attributes with lazy loading

**URL:** <https://www.ruby-forum.com/t/rails-3-associations-extra-attributes-with-lazy-loading/187377>\
**Category:** Rails\
**Created:** [April 22, 2010, 10:22am UTC](https://www.ruby-forum.com/t/rails-3-associations-extra-attributes-with-lazy-loading/187377 "2010-04-22T10:22:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![will\_p](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a87d85/32.png) [@will\_p](https://www.ruby-forum.com/u/will_p)\
**Post date:** [April 22, 2010, 10:22am UTC](https://www.ruby-forum.com/t/rails-3-associations-extra-attributes-with-lazy-loading/187377/1 "2010-04-22T10:22:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![will\_p](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a87d85/32.png) [@will\_p](https://www.ruby-forum.com/u/will_p)\
**Post date:** [April 23, 2010, 2:40am UTC](https://www.ruby-forum.com/t/rails-3-associations-extra-attributes-with-lazy-loading/187377/2 "2010-04-23T02:40:56Z")

</div>

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`’

---

<div class="post-metadata">

**Author:** ![will\_p](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a87d85/32.png) [@will\_p](https://www.ruby-forum.com/u/will_p)\
**Post date:** [April 23, 2010, 5:21am UTC](https://www.ruby-forum.com/t/rails-3-associations-extra-attributes-with-lazy-loading/187377/3 "2010-04-23T05:21:54Z")

</div>

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

2010/4/23 Will P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> =\> “{"created\_at":"2010-04-22T22:52:29Z","updated\_at":  
> "Scissorhands","first\_name":"Edward","email":null}”
> 
> > # Schema
> > 
> > add\_index “consultants\_projects”, [“consultant\_id”], :name =\>  
> > association  
> > ‘consultants\_projects.start\_date,’ +  
> > are stored in the association table.

–  
tommy xiao  
E-mail: xiaods(AT)[gmail.com](http://gmail.com)

---

<div class="post-metadata">

**Author:** ![will\_p](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a87d85/32.png) [@will\_p](https://www.ruby-forum.com/u/will_p)\
**Post date:** [April 23, 2010, 4:19pm UTC](https://www.ruby-forum.com/t/rails-3-associations-extra-attributes-with-lazy-loading/187377/4 "2010-04-23T16:19:53Z")

</div>

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

2010/4/23 tommy xiao [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> > ====================================
> > 
> > ===================================  
> > has\_many :managers, :through =\> :managers\_projects,
> > 
> > > ```
> > > t.date "start_date"
> > > 
> > > ```
> > > 
> > > class Project \< ActiveRecord::Base  
> > > +
> > > 
> > > To unsubscribe from this group, send email to  
> > > removed\_email\_address@domain.invalid[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

–  
tommy xiao  
E-mail: xiaods(AT)[gmail.com](http://gmail.com)

---

<div class="post-metadata">

**Author:** ![will\_p](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a87d85/32.png) [@will\_p](https://www.ruby-forum.com/u/will_p)\
**Post date:** [April 23, 2010, 8:15pm UTC](https://www.ruby-forum.com/t/rails-3-associations-extra-attributes-with-lazy-loading/187377/5 "2010-04-23T20:15:48Z")

</div>

Hi,

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/wprater/76e70c7f31b6dcc5cfcc>

Consultant Model

> <https://gist.github.com/wprater/93e5858417bb14ec18c9>

Project Model

> <https://gist.github.com/wprater/8545e9da49742b4e4dcb>

# Console output using .includes does not honor :select

ruby-1.8.7-p249 \>  
Project.includes(:consultants).first.to\_json(:include =\> :consultants)  
=\> “{"start\_date":null,"name":"Widget Assembly","created\_at":  
"2010-04-22T22:52:29Z","updated\_at":"2010-04-22T22:52:29Z",  
"sales\_contact\_id":null,"id":1,"enabled":true,"client\_id":  
1,"consultants":[{"created\_at":"2010-04-22T22:52:29Z",  
"updated\_at":"2010-04-23T02:35:58Z","recruiter\_contact\_id":  
1,"payclass":2,"id":1,"enabled":true,"phone":"","last\_name  
":"Wright","first\_name":"Steve","email":"removed\_email\_address@domain.invalid  
"}],"end\_date":null}”

# Console output using does honor :select, but uses N + 1 queries for all the consultants in a project

ruby-1.8.7-p249 \> Project.first.to\_json(:include =\> :consultants)  
=\> “{"start\_date":null,"name":"Widget Assembly","created\_at":  
"2010-04-22T22:52:29Z","updated\_at":"2010-04-22T22:52:29Z",  
"sales\_contact\_id":null,"id":1,"enabled":true,"client\_id":  
1,"consultants":[{"start\_date":null,"pay\_rate":"40",  
"created\_at":"2010-04-22T22:52:29Z","updated\_at":  
"2010-04-23T02:35:58Z","recruiter\_contact\_id":1,"project\_id":  
"1","consultants\_projects\_id":"1","bill\_rate":"80","payclass  
":2,"id":1,"enabled":true,"consultants\_projects\_enabled":"t",  
"phone":"","last\_name":"Wright","end\_date":null,"first\_name  
":"Steve","email":"removed\_email\_address@domain.invalid"}],"end\_date":null}”

Any help is appreciated. Am I doing something wrong, or is this just  
not supported?

-Will

---

<div class="post-metadata">

**Author:** ![will\_p](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a87d85/32.png) [@will\_p](https://www.ruby-forum.com/u/will_p)\
**Post date:** [April 24, 2010, 1:40am UTC](https://www.ruby-forum.com/t/rails-3-associations-extra-attributes-with-lazy-loading/187377/6 "2010-04-24T01:40:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![will\_p](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a87d85/32.png) [@will\_p](https://www.ruby-forum.com/u/will_p)\
**Post date:** [April 24, 2010, 2:24pm UTC](https://www.ruby-forum.com/t/rails-3-associations-extra-attributes-with-lazy-loading/187377/7 "2010-04-24T14:24:56Z")

</div>

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

2010/4/24 Will P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> > # =\> "{"start\_date":null,"name":"Widget Assembly","created\_at":
> > 
> > ":"Steve","email":"removed\_email\_address@domain.invalid"}],"end\_date":null}"
> > 
> > > > 2010/4/23 Will P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
> > > > 
> > > > > =\> #\<Manager id: 1, first\_name: “Edward”, last\_name:  
> > > > > updated\_at:  
> > > > > Notice that all the extra attributes are passed  
> > > > > has\_many :managers, :through =\> :managers\_projects,
> > > > > 
> > > > > > # I have a HMT (rails3) association and am using the includes
> > > > > > 
> > > > > > ```
> > > > > > t.integer "bill_rate"
> > > > > > 
> > > > > > ```
> > > > > > 
> > > > > > # Model
> > > > > > 
> > > > > > ```
> > > > > > 'consultants_projects.id as
> > > > > > 
> > > > > > ```
> > 
> > > > > > You received this message because you are subscribed to the Google  
> > > > > > –  
> > > > > > [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).  
> > > > > > You received this message because you are subscribed to the Google  
> > > > > > You received this message because you are subscribed to the Google G.  
> > > > > > “Ruby on Rails: Talk” group.  
> > > > > > To post to this group, send email to removed\_email\_address@domain.invalid.  
> > > > > > To unsubscribe from this group, send email to  
> > > > > > removed\_email\_address@domain.invalid[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> > > > > > .  
> > > > > > For more options, visit this group at  
> > > > > > [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

–  
tommy xiao  
E-mail: xiaods(AT)[gmail.com](http://gmail.com)
