How to get data using JOINS or include

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

I’ve tried

Category.find_by_name(“Main Category 1”).products.find(:all,
:include => :images, :conditions => [‘ 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.


Jordan E.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs