# Database Associations Help

**URL:** <https://www.ruby-forum.com/t/database-associations-help/138582>\
**Category:** Rails\
**Created:** [May 25, 2008, 3:26am UTC](https://www.ruby-forum.com/t/database-associations-help/138582 "2008-05-25T03:26:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![GarrettB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ea5d25/32.png) [@GarrettB](https://www.ruby-forum.com/u/GarrettB)\
**Post date:** [May 25, 2008, 3:26am UTC](https://www.ruby-forum.com/t/database-associations-help/138582/1 "2008-05-25T03:26:41Z")

</div>

So I have a Post model, and a Category model.

I can’t wrap my head around trying to find the number of posts inside  
each category. I believe you would use a join for this, or some sort  
of association.

The “models/category.rb”:

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

The “models/post.rb”  
class Post \< ActiveRecord::Base  
has\_many :comments  
belongs\_to :category  
end

How would I do something like this? I assume you would query it like:  
@category = Category.find(:all)  
@category.post.size

---

<div class="post-metadata">

**Author:** ![GarrettB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ea5d25/32.png) [@GarrettB](https://www.ruby-forum.com/u/GarrettB)\
**Post date:** [May 25, 2008, 7:36am UTC](https://www.ruby-forum.com/t/database-associations-help/138582/2 "2008-05-25T07:36:27Z")

</div>

GarrettB wrote:

> end
> 
> The “models/post.rb”  
> class Post \< ActiveRecord::Base  
> has\_many :comments  
> belongs\_to :category  
> end
> 
> How would I do something like this? I assume you would query it like:

categor = Category.find(:all)

---

<div class="post-metadata">

**Author:** ![GarrettB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ea5d25/32.png) [@GarrettB](https://www.ruby-forum.com/u/GarrettB)\
**Post date:** [May 25, 2008, 7:40am UTC](https://www.ruby-forum.com/t/database-associations-help/138582/3 "2008-05-25T07:40:33Z")

</div>

GarrettB wrote:

> class Category \< ActiveRecord::Base  
> has\_and\_belongs\_to\_many :posts  
> end
> 
> The “models/post.rb”  
> class Post \< ActiveRecord::Base  
> has\_many :comments  
> belongs\_to :category

Both sides of a habtm are always habtm:

has\_and\_belongs\_to\_many :categories

> end
> 
> How would I do something like this? I assume you would query it like:

No @ - there’s no reason to make categories into an instance variable.

categories = Category.find(:all)  
categories.each do |cat|  
p cat.posts.count  
end

Think of cat.posts as simultaneously an array of all posts, and a  
super-model  
that can do anything Post can (such as Post.find()) but with the link to  
the  
Category automatically turned on.

---

<div class="post-metadata">

**Author:** ![GarrettB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ea5d25/32.png) [@GarrettB](https://www.ruby-forum.com/u/GarrettB)\
**Post date:** [May 25, 2008, 8:31am UTC](https://www.ruby-forum.com/t/database-associations-help/138582/4 "2008-05-25T08:31:24Z")

</div>

I can’t seem to for the life of me to get this working, do you have an  
example of how my database should look? And how my associations need  
to be?
