# Find all object through another object - how?

**URL:** <https://www.ruby-forum.com/t/find-all-object-through-another-object-how/118960>\
**Category:** Rails\
**Created:** [November 1, 2007, 7:20pm UTC](https://www.ruby-forum.com/t/find-all-object-through-another-object-how/118960 "2007-11-01T19:20:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [November 1, 2007, 7:20pm UTC](https://www.ruby-forum.com/t/find-all-object-through-another-object-how/118960/1 "2007-11-01T19:20:19Z")

</div>

I’m trying to find all “apples” that belong to user\_id 2.

But the “apples” table doesn’t have a “user\_id” column; it has a  
“fruit\_id” column, that points to a “fruit” object.

The “fruit” table has a “user\_id” column.

So how would I set something up like this to find all the “apples” that  
belong to user\_id 2?

@apples = Apple.find(:all, :conditions =\> [“fruit.user\_id = 2”])

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [November 1, 2007, 8:09pm UTC](https://www.ruby-forum.com/t/find-all-object-through-another-object-how/118960/2 "2007-11-01T20:09:20Z")

</div>

Joe P. wrote:

> I’m trying to find all “apples” that belong to user\_id 2.
> 
> But the “apples” table doesn’t have a “user\_id” column; it has a  
> “fruit\_id” column, that points to a “fruit” object.
> 
> The “fruit” table has a “user\_id” column.
> 
> So how would I set something up like this to find all the “apples” that  
> belong to user\_id 2?
> 
> @apples = Apple.find(:all, :conditions =\> [“fruit.user\_id = 2”])

Just in case someone’s wondering, I already have the right connections  
in the models (like :belongs\_to, :has\_many). So that’s not the problem.

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [November 1, 2007, 8:20pm UTC](https://www.ruby-forum.com/t/find-all-object-through-another-object-how/118960/3 "2007-11-01T20:20:59Z")

</div>

On 1 Nov 2007, at 19:09, Joe P. wrote:

> > that  
> > belong to user\_id 2?
> > 
> > @apples = Apple.find(:all, :conditions =\> [“fruit.user\_id = 2”])
> 
> Just in case someone’s wondering, I already have the right connections  
> in the models (like :belongs\_to, :has\_many). So that’s not the  
> problem.  
> Well with a has\_many :through you could just do User.find(2).apples  
> If you don’t want to do that then you’ll have to write down the  
> appropriate join.

Fred

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [November 1, 2007, 10:44pm UTC](https://www.ruby-forum.com/t/find-all-object-through-another-object-how/118960/4 "2007-11-01T22:44:57Z")

</div>

On 11/1/07, Joe P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> @apples = Apple.find(:all, :conditions =\> [“fruit.user\_id = 2”])

You need to join the fruits table to the query. One way to do this is

@apples = Apple.find(:all, :include =\> :fruits, ::conditions =\>  
[“fruit.user\_id = 2”])

You could also use a :joins string to manual add the sql join clause,  
but this has the side effect of making the resultant Apple read only  
since it will contain bogus fields from the fruit table.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
