# Problem using ActiveRecord find with :joins and :select

**URL:** <https://www.ruby-forum.com/t/problem-using-activerecord-find-with-joins-and-select/176697>\
**Category:** Rails\
**Created:** [October 5, 2009, 1:21am UTC](https://www.ruby-forum.com/t/problem-using-activerecord-find-with-joins-and-select/176697 "2009-10-05T01:21:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![PierreW](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/da6949/32.png) [@PierreW](https://www.ruby-forum.com/u/PierreW)\
**Post date:** [October 5, 2009, 1:21am UTC](https://www.ruby-forum.com/t/problem-using-activerecord-find-with-joins-and-select/176697/1 "2009-10-05T01:21:27Z")

</div>

Hi guys,

When I do:

temp = User.find(:all,  
:joins =\> “INNER JOIN contents ON users.agent\_id =  
contents.id”,  
:select =\> “contents.id, users.id, users.u\_date”)

temp.first.inspect

It seems to work fine, but it only returns:  
#\<User id: 6, u\_date: “2009-10-03 19:32:23”\>

but not the contents.id information.

I don’t get it, and I don’t know what to do to rectify this. It seems  
like since I call find on User, it only returns infos related to this  
model.  
By the way, when I copy the SQL code generated by Rails directly in my  
DB, it works fine and returns all 3 fields.

Do you have any idea what I am missing?

Thanks a lot!  
Peter

---

<div class="post-metadata">

**Author:** ![PierreW](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/da6949/32.png) [@PierreW](https://www.ruby-forum.com/u/PierreW)\
**Post date:** [October 5, 2009, 5:07am UTC](https://www.ruby-forum.com/t/problem-using-activerecord-find-with-joins-and-select/176697/2 "2009-10-05T05:07:58Z")

</div>

Hiii  
Active record → mapping rec on table to object

SO…

temp.first.inspect → _user object_

please try… temp.first.content.inspect—\> will show contents.id

Note: first you must add relation between model user and model  
content…

Thank you

On Mon, Oct 5, 2009 at 6:19 AM, PierreW [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> temp.first.inspect  
> DB, it works fine and returns all 3 fields.
> 
> Do you have any idea what I am missing?
> 
> Thanks a lot!  
> Peter
> 
> >

–

Senior Rails Developer  
Anton Effendi - Wu You Duan

---

<div class="post-metadata">

**Author:** ![PierreW](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/da6949/32.png) [@PierreW](https://www.ruby-forum.com/u/PierreW)\
**Post date:** [October 5, 2009, 9:39am UTC](https://www.ruby-forum.com/t/problem-using-activerecord-find-with-joins-and-select/176697/3 "2009-10-05T09:39:34Z")

</div>

On Oct 5, 12:19 am, PierreW [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Do you have any idea what I am missing?

attributes are stored in a hash, ie there can be only one attribute  
called id - you need to alias the contents.id attribute. Even when you  
do that, inspect won’t show the extra value, but temp.first.content\_id  
(assuming you alias it as content\_id) should return the right id.  
Lastly, what’s the point of the hoin to get contents.id when you know  
the contents.id == users.agent\_id ?

Fred

---

<div class="post-metadata">

**Author:** ![PierreW](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/da6949/32.png) [@PierreW](https://www.ruby-forum.com/u/PierreW)\
**Post date:** [October 6, 2009, 12:14am UTC](https://www.ruby-forum.com/t/problem-using-activerecord-find-with-joins-and-select/176697/4 "2009-10-06T00:14:17Z")

</div>

Thanks a lot guys for clarifying this. It worked with the aliases as  
you suggested.

Fred, to your last point: sorry about that, you are right. I just made  
up the example to show the mechanics, I didn’t realize it didn’t quite  
make any sense.

Peter

On Oct 5, 8:37 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
