# Searching through HABTM

**URL:** <https://www.ruby-forum.com/t/searching-through-habtm/138825>\
**Category:** Rails\
**Created:** [May 28, 2008, 4:24am UTC](https://www.ruby-forum.com/t/searching-through-habtm/138825 "2008-05-28T04:24:58Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jables](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/58956e/32.png) [@Jables](https://www.ruby-forum.com/u/Jables)\
**Post date:** [May 28, 2008, 4:24am UTC](https://www.ruby-forum.com/t/searching-through-habtm/138825/1 "2008-05-28T04:24:58Z")

</div>

Hey,  
I have a model “story” that habtm genres (model - genre, table -  
genres, join\_table genres\_stories). How do I look for a story of a  
certain genre in a find condition?

JB

---

<div class="post-metadata">

**Author:** ![Jables](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/58956e/32.png) [@Jables](https://www.ruby-forum.com/u/Jables)\
**Post date:** [May 28, 2008, 4:39am UTC](https://www.ruby-forum.com/t/searching-through-habtm/138825/2 "2008-05-28T04:39:03Z")

</div>

> I have a model “story” that habtm genres (model - genre, table -  
> genres, join\_table genres\_stories). How do I look for a story of a  
> certain genre in a find condition?

story = Story.find(42)  
genres = story.find\_all\_by\_name(‘whimsy’)  
more\_genres = story.find(:all, :conditions = ‘name like “whim%”’)

Now read

> **[ActiveRecord::Associations::ClassMethods](https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html)**
>
> Associations are a set of macro-like class methods for tying objects together through foreign keys.

there’s a reason that page lists all the various (mostly boring) methods  
that automatically get generated when you add an association!

---

<div class="post-metadata">

**Author:** ![Jables](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/58956e/32.png) [@Jables](https://www.ruby-forum.com/u/Jables)\
**Post date:** [May 28, 2008, 6:39am UTC](https://www.ruby-forum.com/t/searching-through-habtm/138825/3 "2008-05-28T06:39:31Z")

</div>

I may be misunderstanding what you are saying, but what I am looking  
to do is essentially:  
stories = Story.find\_all\_by\_genres(“GENRE”)  
where genres has a has\_and\_belongs\_to\_many relationship to story.

Thanks,  
JB

---

<div class="post-metadata">

**Author:** ![Jables](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/58956e/32.png) [@Jables](https://www.ruby-forum.com/u/Jables)\
**Post date:** [May 28, 2008, 5:57pm UTC](https://www.ruby-forum.com/t/searching-through-habtm/138825/4 "2008-05-28T17:57:17Z")

</div>

On Wed, May 28, 2008 at 12:38 AM, Jables [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

class Story \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :genres  
end

class Genre \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :stories  
end

Genre.find(“mystery”).stories

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![Jables](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/58956e/32.png) [@Jables](https://www.ruby-forum.com/u/Jables)\
**Post date:** [May 30, 2008, 5:38am UTC](https://www.ruby-forum.com/t/searching-through-habtm/138825/5 "2008-05-30T05:38:43Z")

</div>

Thanks for the info guys,  
Genre.find\_by\_name(“genre”).stories is close to what I want. Let me  
make it a bit more complex though now:  
I want to be able to take those stories of a certain genre and order  
them by rating or post date, and filter them by another factor which  
is category (and maybe even another parameter). So Story also HABTM  
categories and has a rating/post\_date parameter. As the number of  
stories in a genre is going to grow largely it seems wildly  
inefficient to take the array of all the stories then increment though  
each one check to see if it is in the right category and sorting by  
date. What I really need is something like:

Story.find\_by\_genre\_and\_category\_and\_anotherthing(“genre”, “category”,  
“anotherthing”, :conditions =\> “post\_date \< somedate”, :order =\>  
“rating DESC”)

Anybody have any ideas?

Thanks,  
JB

---

<div class="post-metadata">

**Author:** ![Jables](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/58956e/32.png) [@Jables](https://www.ruby-forum.com/u/Jables)\
**Post date:** [May 30, 2008, 6:12am UTC](https://www.ruby-forum.com/t/searching-through-habtm/138825/6 "2008-05-30T06:12:05Z")

</div>

> Story.find\_by\_genre\_and\_category\_and\_anotherthing(“genre”, “category”,  
> “anotherthing”, :conditions =\> “post\_date \< somedate”, :order =\>  
> “rating DESC”)

Looks good. Try .find\_all\_by\_…

If anotherthing is in another table, you will need to :include =\>  
:anotherthings, and you can’t use an automatic method named _and_.

---

<div class="post-metadata">

**Author:** ![Jables](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/58956e/32.png) [@Jables](https://www.ruby-forum.com/u/Jables)\
**Post date:** [May 28, 2008, 5:57pm UTC](https://www.ruby-forum.com/t/searching-through-habtm/138825/7 "2008-05-28T17:57:17Z")

</div>

On Wed, May 28, 2008 at 11:54 AM, Rick DeNatale  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > >
> 
> Genre.find(“mystery”).stories

Of course, that should have been something like

Genre.find\_by\_name(“mystery”).stories

Assuming that Genre has a name attribute.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![Jables](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/58956e/32.png) [@Jables](https://www.ruby-forum.com/u/Jables)\
**Post date:** [May 30, 2008, 5:36pm UTC](https://www.ruby-forum.com/t/searching-through-habtm/138825/8 "2008-05-30T17:36:51Z")

</div>

> The problem I have is that f.find\_all\_by\_ doesn’t work. I have tried  
> find\_all\_by\_genre and I get:  
> undefined method `find\_all\_by\_genre’  
> I tried find\_all\_by\_genre(“GENRE”, :include =\> :genres and no dice.

> > If anotherthing is in another table, you will need to :include =\>  
> > :anotherthings, and you can’t use an automatic method named _and_.

Read it again.

---

<div class="post-metadata">

**Author:** ![Jables](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/58956e/32.png) [@Jables](https://www.ruby-forum.com/u/Jables)\
**Post date:** [May 30, 2008, 6:34am UTC](https://www.ruby-forum.com/t/searching-through-habtm/138825/9 "2008-05-30T06:34:19Z")

</div>

The problem I have is that f.find\_all\_by\_ doesn’t work. I have tried  
find\_all\_by\_genre and I get:  
undefined method `find\_all\_by\_genre’  
I tried find\_all\_by\_genre(“GENRE”, :include =\> :genres and no dice.

JB
