# Finding items in a Join Table

**URL:** <https://www.ruby-forum.com/t/finding-items-in-a-join-table/156482>\
**Category:** Rails\
**Created:** [December 28, 2008, 7:35pm UTC](https://www.ruby-forum.com/t/finding-items-in-a-join-table/156482 "2008-12-28T19:35:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rembrant](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@Rembrant](https://www.ruby-forum.com/u/Rembrant)\
**Post date:** [December 28, 2008, 7:35pm UTC](https://www.ruby-forum.com/t/finding-items-in-a-join-table/156482/1 "2008-12-28T19:35:28Z")

</div>

Dear readers,

I’m very new to RoR, I walked trough the Agile Web D. With  
Rails book, but I still got stuck. I’m trained as a designer, so  
coding isn’t really my branch but I try to get by. Anyways, here is my  
question.

I set up 3 tables including one join table like this:

class Category \< ActiveRecord::Base  
has\_many :products  
has\_many :brands, :through =\> :products, :uniq =\> true  
end

class Brand \< ActiveRecord::Base  
has\_many :products  
has\_many :categories, :through =\> :products  
end

class Product \< ActiveRecord::Base  
belongs\_to :category  
belongs\_to :brand  
end

I can easily find brands belonging to a certain category doing this:

@brands = Category.find(session[:selected\_category]).brands

(it gets the session var from a link\_to\_remote’s params[:id])

But now I’m trying to find all the products that belong to a certain  
brand and a certain category, so for example only Laptops (category)  
made by Apple (brand). This is the code I’ve got this far, but I’m  
sure there must be something more elegant and something that just  
works better, because this code seems to fail frequently:

@products = Product.find(:all, :conditions =\> “category\_id = ‘#{session  
[:selected\_category].id}’ AND brand\_id = ‘#{session  
[:selected\_brand].id}’”)

This doesn’t look too crazy from what I remember from mysql, but I bet  
there is something way Rubier.

Any help would be greatly appreciated.

Kindest regards,

- Rembrant

---

<div class="post-metadata">

**Author:** ![Rembrant](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@Rembrant](https://www.ruby-forum.com/u/Rembrant)\
**Post date:** [December 28, 2008, 8:12pm UTC](https://www.ruby-forum.com/t/finding-items-in-a-join-table/156482/2 "2008-12-28T20:12:57Z")

</div>

This _might_ work… it’s worth trying at least. 😉

@products = Brand.find( brand\_id ).categories( category\_id ).products

On Sun, Dec 28, 2008 at 9:25 AM, Rembrant  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> works better, because this code seems to fail frequently:  
> there is something way Rubier.
> 
> Any help would be greatly appreciated.
> 
> Kindest regards,
> 
> - Rembrant
> 
> >

–  
Robby R.  
Chief Evangelist, Partner

PLANET ARGON, LLC  
design // development // hosting w/Ruby on Rails

> **[Planet Argon](https://www.planetargon.com/)**
>
> Planet Argon is a Ruby on Rails development agency specializing in maintenance and support for existing Rails apps. Based in Portland, OR, Planet Argon has been a member of the Rails community since 2004.

[http://www.robbyonrails.com/](http://www.robbyonrails.com/)  
aim: planetargon

+1 503 445 2457  
+1 877 55 ARGON [toll free]  
+1 815 642 4068 [fax]
