# Getting all records in a relationship - how can I do this in

**URL:** <https://www.ruby-forum.com/t/getting-all-records-in-a-relationship-how-can-i-do-this-in/109783>\
**Category:** Rails\
**Created:** [July 19, 2007, 12:46am UTC](https://www.ruby-forum.com/t/getting-all-records-in-a-relationship-how-can-i-do-this-in/109783 "2007-07-19T00:46:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@rob](https://www.ruby-forum.com/u/rob)\
**Post date:** [July 19, 2007, 12:46am UTC](https://www.ruby-forum.com/t/getting-all-records-in-a-relationship-how-can-i-do-this-in/109783/1 "2007-07-19T00:46:51Z")

</div>

[Parked at Loopia](http://pastie.caboo.se/79914) - i’d like to get all of the graphics of  
a certain _category_ (not theme) by specifying the category id, but  
none of the graphics are associated with a category\_id, just a  
theme\_id. I included a solution I wrote that works fine, but it looks  
incredibly ugly and there has to be a “cleaner” way. Any ideas?

---

<div class="post-metadata">

**Author:** ![rob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@rob](https://www.ruby-forum.com/u/rob)\
**Post date:** [July 19, 2007, 12:58am UTC](https://www.ruby-forum.com/t/getting-all-records-in-a-relationship-how-can-i-do-this-in/109783/2 "2007-07-19T00:58:52Z")

</div>

> [Parked at Loopia](http://pastie.caboo.se/79914) - i’d like to get all of the graphics of  
> a certain _category_ (not theme) by specifying the category id, but  
> none of the graphics are associated with a category\_id, just a  
> theme\_id. I included a solution I wrote that works fine, but it looks  
> incredibly ugly and there has to be a “cleaner” way. Any ideas?

Graphic.find(:all, :include =\> [:themes =\> :categories],  
:conditions =\> [“categories.id = ?”, params[:id]])

Or something close to that.

---

<div class="post-metadata">

**Author:** ![rob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@rob](https://www.ruby-forum.com/u/rob)\
**Post date:** [July 19, 2007, 1:27am UTC](https://www.ruby-forum.com/t/getting-all-records-in-a-relationship-how-can-i-do-this-in/109783/3 "2007-07-19T01:27:57Z")

</div>

you could alternatively define your category model as such

class Category \< ActiveRecord::Base  
has\_many :themes  
has\_many :graphics, :through =\> :themes  
end

then;  
@category = Category.find(id, :include =\> :graphics)  
@graphics = @category.graphics.

Philip H. wrote:

> > [Parked at Loopia](http://pastie.caboo.se/79914) - i’d like to get all of the graphics of  
> > a certain _category_ (not theme) by specifying the category id, but  
> > none of the graphics are associated with a category\_id, just a  
> > theme\_id. I included a solution I wrote that works fine, but it looks  
> > incredibly ugly and there has to be a “cleaner” way. Any ideas?
> 
> Graphic.find(:all, :include =\> [:themes =\> :categories],  
> :conditions =\> [“categories.id = ?”, params[:id]])
> 
> Or something close to that.
