# Has\_and\_belong\_to\_many relation

**URL:** <https://www.ruby-forum.com/t/has-and-belong-to-many-relation/146515>\
**Category:** Rails\
**Created:** [August 21, 2008, 10:21pm UTC](https://www.ruby-forum.com/t/has-and-belong-to-many-relation/146515 "2008-08-21T22:21:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pipex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f6c823/32.png) [@pipex](https://www.ruby-forum.com/u/pipex)\
**Post date:** [August 21, 2008, 10:21pm UTC](https://www.ruby-forum.com/t/has-and-belong-to-many-relation/146515/1 "2008-08-21T22:21:18Z")

</div>

First of all i have a conceptual doubt, I have 2 tables, post and user  
and  
both have many to many relation. Ok i do put in both models  
Post Model  
has\_and\_belongs\_to\_many :users  
Users Model  
has\_and\_belongs\_to\_many :posts

after this i’ved create posts\_users model with id\_user and id\_post and  
a  
table for that, is this right???

Then another cuestion I want that when a post is created, generate a row  
in  
the posts\_users table with the id\_user and the id\_post.

If anyone can help please thank you very much.

–  
Felipe V. Contesse  
IngenierÃ­a Civil Industrial UC

---

<div class="post-metadata">

**Author:** ![pipex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f6c823/32.png) [@pipex](https://www.ruby-forum.com/u/pipex)\
**Post date:** [August 22, 2008, 1:51am UTC](https://www.ruby-forum.com/t/has-and-belong-to-many-relation/146515/2 "2008-08-22T01:51:10Z")

</div>

Felipe V. wrote:

> First of all i have a conceptual doubt, I have 2 tables, post and user  
> and  
> both have many to many relation. Ok i do put in both models  
> Post Model  
> has\_and\_belongs\_to\_many :users  
> Users Model  
> has\_and\_belongs\_to\_many :posts
> 
> after this i’ved create posts\_users model with id\_user and id\_post and  
> a  
> table for that, is this right???

You don’t need the intermediate model if it has no further attributes.  
You just need a join table.

create\_table :users\_posts, :id =\> false do |t|  
t.integer :user\_id  
t.integer :post\_id  
end

You should probably also create indices and unique constraints to this  
table, but that’s fairly advanced.

> Then another cuestion I want that when a post is created, generate a row  
> in  
> the posts\_users table with the id\_user and the id\_post.

This is automatically done for you if you set the model properties,  
e.g.:

an\_user.posts \<\< a\_post  
an\_user.save

> If anyone can help please thank you very much.

Hope that helped,  
GG

> –  
> Felipe V. Contesse  
> IngenierÃ­a Civil Industrial UC

---

<div class="post-metadata">

**Author:** ![pipex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f6c823/32.png) [@pipex](https://www.ruby-forum.com/u/pipex)\
**Post date:** [August 22, 2008, 1:53am UTC](https://www.ruby-forum.com/t/has-and-belong-to-many-relation/146515/3 "2008-08-22T01:53:51Z")

</div>

On 22 Aug 2008, at 00:51, Go Gormo wrote:

> > 
> 
> t.integer :post\_id  
> end

That does actually need to be posts\_users (alphabetic order). You can  
of course override the name of the join table to be anything you want  
but there’s no reason to if you don’t need to.

Fred

---

<div class="post-metadata">

**Author:** ![pipex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f6c823/32.png) [@pipex](https://www.ruby-forum.com/u/pipex)\
**Post date:** [August 22, 2008, 3:13pm UTC](https://www.ruby-forum.com/t/has-and-belong-to-many-relation/146515/4 "2008-08-22T15:13:09Z")

</div>

thak you very much!!! that helped me a lot i have one last cuestion

where do i put this??  
an\_user.posts \<\< a\_post  
an\_user.save  
in the users model?  
On Thu, Aug 21, 2008 at 7:53 PM, Frederick C. \<  
removed\_email\_address@domain.invalid\> wrote:

> > > Post Model  
> > > You just need a join table.  
> > > Fred
> 
> >

–  
Felipe V. Contesse  
IngenierÃ­a Civil Industrial UC

---

<div class="post-metadata">

**Author:** ![pipex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f6c823/32.png) [@pipex](https://www.ruby-forum.com/u/pipex)\
**Post date:** [August 22, 2008, 4:41pm UTC](https://www.ruby-forum.com/t/has-and-belong-to-many-relation/146515/5 "2008-08-22T16:41:47Z")

</div>

in the controller (i guess)
