# HABTM query

**URL:** <https://www.ruby-forum.com/t/habtm-query/118702>\
**Category:** Rails\
**Created:** [October 30, 2007, 8:46am UTC](https://www.ruby-forum.com/t/habtm-query/118702 "2007-10-30T08:46:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![danywu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@danywu](https://www.ruby-forum.com/u/danywu)\
**Post date:** [October 30, 2007, 8:46am UTC](https://www.ruby-forum.com/t/habtm-query/118702/1 "2007-10-30T08:46:11Z")

</div>

Hi everyone,

I am just playing with a Blog project at the moment. It has a simple  
data model of

Post  
has\_and\_belongs\_to\_many :categories

Category  
has\_and\_belongs\_to\_many :posts

And the DB tables to serve the model. It’s all working fine and dandy so  
far. I want to add a listing to filter by category - here’s where I’m a  
bit stuck on the syntax.

How do I get all the posts that belongs to a certain category? I’m  
having problems figuring how to write out the condition.

Any hits/suggestions/answers would be greatly appreciated.

Many thanks,  
Dany.

---

<div class="post-metadata">

**Author:** ![danywu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@danywu](https://www.ruby-forum.com/u/danywu)\
**Post date:** [October 30, 2007, 8:49am UTC](https://www.ruby-forum.com/t/habtm-query/118702/2 "2007-10-30T08:49:02Z")

</div>

Sorry, forgot to ask one more thing. I have listed the Categories under  
the Post, and made each one of the a link to the action  
list\_by\_category. I can’t quite figure out how to pass the value of the  
category from the view back to the controller to be used in the query.  
This is assuming I need to do this, of course…

Thanks,  
Dany.

---

<div class="post-metadata">

**Author:** ![danywu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@danywu](https://www.ruby-forum.com/u/danywu)\
**Post date:** [October 30, 2007, 11:53am UTC](https://www.ruby-forum.com/t/habtm-query/118702/3 "2007-10-30T11:53:19Z")

</div>

On 30 Oct 2007, at 07:49, Dany Wu wrote:

> Sorry, forgot to ask one more thing. I have listed the Categories  
> under  
> the Post, and made each one of the a link to the action  
> list\_by\_category. I can’t quite figure out how to pass the value of  
> the  
> category from the view back to the controller to be used in the query.  
> This is assuming I need to do this, of course…

You need to pass the id of the category

You can then do (in your list\_by\_category action) @posts =  
Category.find(params[:id]).posts.

---

<div class="post-metadata">

**Author:** ![danywu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@danywu](https://www.ruby-forum.com/u/danywu)\
**Post date:** [October 30, 2007, 12:03pm UTC](https://www.ruby-forum.com/t/habtm-query/118702/4 "2007-10-30T12:03:19Z")

</div>

Frederick C. wrote:

> On 30 Oct 2007, at 07:49, Dany Wu wrote:
> 
> > Sorry, forgot to ask one more thing. I have listed the Categories  
> > under  
> > the Post, and made each one of the a link to the action  
> > list\_by\_category. I can’t quite figure out how to pass the value of  
> > the  
> > category from the view back to the controller to be used in the query.  
> > This is assuming I need to do this, of course…
> 
> You need to pass the id of the category
> 
> You can then do (in your list\_by\_category action) @posts =  
> Category.find(params[:id]).posts.

Perfect! Thanks for that - I’ve just figured out custom pagination, and  
it’s all go!

D.
