# How to get data using JOINS or include

**URL:** <https://www.ruby-forum.com/t/how-to-get-data-using-joins-or-include/133379>\
**Category:** Rails\
**Created:** [March 31, 2008, 1:51am UTC](https://www.ruby-forum.com/t/how-to-get-data-using-joins-or-include/133379 "2008-03-31T01:51:22Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jordelver](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@jordelver](https://www.ruby-forum.com/u/jordelver)\
**Post date:** [March 31, 2008, 1:51am UTC](https://www.ruby-forum.com/t/how-to-get-data-using-joins-or-include/133379/1 "2008-03-31T01:51:22Z")

</div>

Hi,  
I’m having trouble getting the records I need from ActiveRecord.

I have a category model which is using acts\_as\_tree. I have two levels  
of categories  
currently. So, I have something like this:

Main Category 1  
|- Sub Category 1  
|- Sub Category 2

Main Category 2  
|- Sub Category 1  
|- Sub Category 2

Products belong to 1 or many of the sub categories. I’m trying to get  
3 records from  
within either of the main categories. So, for example, I want 3  
records from “Main Category 1”.  
I also have many images associated with each product using  
attachment\_fu.

I’ve tried

Category.find\_by\_name(“Main Category 1”).products.find(:all,  
:include =\> :images, :conditions =\> [‘products.id IN (6,7,8)’])

But this doesn’t work because no products actually exist in that  
category, but  
they do in its sub categories. I know I can do a JOIN manually, but then  
I don’t  
get all the data back as the correct objects, just as extra attributes  
of the  
Product model.

Any tips or pointers regarding how to do this would be appreciated.

## Thanks, Jordan

Jordan E.
