# ActiveRecord::Base.find returning columns from :joins

**URL:** <https://www.ruby-forum.com/t/activerecord-base-find-returning-columns-from-joins/107208>\
**Category:** Rails\
**Created:** [June 19, 2007, 6:41am UTC](https://www.ruby-forum.com/t/activerecord-base-find-returning-columns-from-joins/107208 "2007-06-19T06:41:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dutone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@dutone](https://www.ruby-forum.com/u/dutone)\
**Post date:** [June 19, 2007, 6:41am UTC](https://www.ruby-forum.com/t/activerecord-base-find-returning-columns-from-joins/107208/1 "2007-06-19T06:41:30Z")

</div>

Hi,

I’m trying to find all of the products that have parts supplied by  
the given supplier’s name:

@products=Product.find :all, :include=\>[“company”,“parts”],  
:joins=\>“inner join suppliers\_to\_parts  
stp on stp.part = parts.id  
inner join suppliers s on s.id =  
stp.supplier”,  
:conditions=\>[“s.name = ?”,name]

The problem is that the supplier’s name is not returned, and :select  
doesn’t work when :include is used.  
Of course I could use all :joins, or even find\_by\_sql, but I’d like to  
have my object hierarchy (well, as much as rails will allow) returned  
instead of a single object with a bunch of attributes.

Is it possible to achieve this some other way?

Thank You

---

<div class="post-metadata">

**Author:** ![dutone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@dutone](https://www.ruby-forum.com/u/dutone)\
**Post date:** [June 19, 2007, 10:31am UTC](https://www.ruby-forum.com/t/activerecord-base-find-returning-columns-from-joins/107208/2 "2007-06-19T10:31:55Z")

</div>

On 6/18/07, dutone [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> inner join suppliers s on s.id =
> 
> ```
> 
> Thank You

Nope, eager includes clobber the :select option because of the way it  
generates it’s own select clause.

–  
Rick O.  
[http://lighthouseapp.com](http://lighthouseapp.com)  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)

---

<div class="post-metadata">

**Author:** ![dutone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@dutone](https://www.ruby-forum.com/u/dutone)\
**Post date:** [June 20, 2007, 4:15am UTC](https://www.ruby-forum.com/t/activerecord-base-find-returning-columns-from-joins/107208/3 "2007-06-20T04:15:20Z")

</div>

On Jun 19, 8:11 am, Ryan S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I was having the same issue earlier this week – innner joining two  
> tables while trying to return a set of AR objects for one of those  
> tables. Here’s what I did in my project to get it to work.
> 
> [Parked at Loopia](http://pastie.caboo.se/71756)

Yup, I’m afraid thats what i’ll have to do.  
Thanks

---

<div class="post-metadata">

**Author:** ![dutone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@dutone](https://www.ruby-forum.com/u/dutone)\
**Post date:** [June 19, 2007, 5:12pm UTC](https://www.ruby-forum.com/t/activerecord-base-find-returning-columns-from-joins/107208/4 "2007-06-19T17:12:59Z")

</div>

I was having the same issue earlier this week – innner joining two  
tables while trying to return a set of AR objects for one of those  
tables. Here’s what I did in my project to get it to work.

[http://pastie.caboo.se/71756](http://pastie.caboo.se/71756)

Granted I’m using paginate() rather than find(), but it should  
probably work for you. I think the important thing here is the not  
use the :include option. As Rick confirmed, it clobbers the :select  
option with it’s own value.

– RYAN
