Joshua Novak
Joshua Novak

Reputation: 144

How can i filter a resource and it's nested resources in rails 3?

I have categories and each category has resources that belong to them. I want to use a scope on all categories and a different scope on their resources.

This is an example of what i am trying to do:

categories = Category.find_all_by_name('something').resources.find_all_by_resource_type_id(3)

Upvotes: 1

Views: 703

Answers (1)

Andrei S
Andrei S

Reputation: 6516

your post ain't exactly crystal clear

my guess is that you probably want the categories that have "something" name, and their resources eager-loaded. for that use this:

Category.where(:name => 'something').includes(:resources)

or, if you want to specify what resources you want for those "something" categories, you can use this:

Category.includes(:resources).where(:name => 'something', :resources => {:resource_type_id => 3})

more info about active record querying, here

Upvotes: 1

Related Questions