# HABTM & find\_all\_by

**URL:** <https://www.ruby-forum.com/t/habtm-find-all-by/167930>\
**Category:** Rails\
**Created:** [May 14, 2009, 3:30am UTC](https://www.ruby-forum.com/t/habtm-find-all-by/167930 "2009-05-14T03:30:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![akzidenz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/48db29/32.png) [@akzidenz](https://www.ruby-forum.com/u/akzidenz)\
**Post date:** [May 14, 2009, 3:30am UTC](https://www.ruby-forum.com/t/habtm-find-all-by/167930/1 "2009-05-14T03:30:57Z")

</div>

Hello!

I’m a little new to rails and have a strange problem i’m trying to  
decipher.

I have a Project model and Category model, connected together by a HABTM  
(with a categorys\_projects join table)

Rails is doing everything as it should, apart from the find.

Am I wrong to think that I should be able to go:-

projects = Project.find\_all\_by\_category(some\_id)

As the only way I can return projects of a certain category right now is  
using  
a join like so :

projects = Project.find(:all, joins =\> :categories, :conditions =\>  
{:categories =\> { :id =\> params[:category] } })

Where:-

class Project \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :categories  
end

class Category \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :projects  
end

Bit confused!

Any help would be great

Thanks!  
K

---

<div class="post-metadata">

**Author:** ![akzidenz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/48db29/32.png) [@akzidenz](https://www.ruby-forum.com/u/akzidenz)\
**Post date:** [May 14, 2009, 9:41am UTC](https://www.ruby-forum.com/t/habtm-find-all-by/167930/2 "2009-05-14T09:41:44Z")

</div>

Hi

how about this?

category = Category.find some\_id  
projects = category.projects

projects = Project.find\_all\_by\_category(some\_id) will not work,  
because find\_all\_by\_… methods work only for existing columns in  
table

Hope it helps )

---

<div class="post-metadata">

**Author:** ![akzidenz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/48db29/32.png) [@akzidenz](https://www.ruby-forum.com/u/akzidenz)\
**Post date:** [May 14, 2009, 12:24pm UTC](https://www.ruby-forum.com/t/habtm-find-all-by/167930/3 "2009-05-14T12:24:13Z")

</div>

Bob wrote:

> Hi
> 
> how about this?
> 
> category = Category.find some\_id  
> projects = category.projects
> 
> projects = Project.find\_all\_by\_category(some\_id) will not work,  
> because find\_all\_by\_… methods work only for existing columns in  
> table
> 
> Hope it helps )

I think I understand what you’re saying!

Thanks a bunch!
