# HABTM 3 table relation

**URL:** <https://www.ruby-forum.com/t/habtm-3-table-relation/156029>\
**Category:** Rails\
**Created:** [December 19, 2008, 2:27pm UTC](https://www.ruby-forum.com/t/habtm-3-table-relation/156029 "2008-12-19T14:27:09Z")\
**Posts on this page:** 3\
**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:** [December 19, 2008, 2:27pm UTC](https://www.ruby-forum.com/t/habtm-3-table-relation/156029/1 "2008-12-19T14:27:09Z")

</div>

Some help please!!  
class Article \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :tags, :join\_table =\> :articles\_tags\_users  
has\_and\_belongs\_to\_many :users, :join\_table =\> :articles\_tags\_users  
end

class Tag \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :articles, :join\_table =\> :articles\_tags\_users  
has\_and\_belongs\_to\_many :users, :join\_table =\> :articles\_tags\_users  
end

class User \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :articles, :join\_table =\> :articles\_tags\_users  
has\_and\_belongs\_to\_many :tags, :join\_table =\> :articles\_tags\_users  
end

then in the controller

user = User.new  
tag = Tag.find(id)  
article = Article.find(id)  
user.tag \<\< tag  
user.article \<\< article

all this creates 2 tuples on the database articles\_tags\_users instead of  
one  
with all the values

Do somebody knows how to do this?

## Thanks !

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:** [December 20, 2008, 6:52am UTC](https://www.ruby-forum.com/t/habtm-3-table-relation/156029/2 "2008-12-20T06:52:04Z")

</div>

On Dec 19, 8:26 am, “Felipe V.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Some help please!!  
> class Article \< ActiveRecord::Base  
> has\_and\_belongs\_to\_many :tags, :join\_table =\> :articles\_tags\_users  
> […]  
> user = User.new  
> tag = Tag.find(id)  
> article = Article.find(id)  
> user.tag \<\< tag  
> user.article \<\< article
> 
> all this creates 2 tuples on the database articles\_tags\_users instead of one  
> with all the values
> 
> Do somebody knows how to do this?

I hope someone will correct me if I’m wrong, but I believe you will  
need to use has\_many :through for this case – HABTM isn’t meant for 3-  
way joins as far as I know. You’ll need something like this:

class Article \< ActiveRecord::Base  
has\_many :correlations  
has\_many :tags, :through =\> :correlations  
has\_many :users, :through =\> :correlations  
end

(similarly for User and Tag)

class Correlation \< ActiveRecord::Base  
belongs\_to :article  
belongs\_to :tag  
belongs\_to :user  
end

Then your sample controller code would become something like

user = User.new  
tag = Tag.find(id)  
article = Article.find(id)  
user.correlations \<\< Correlation.new(:tag =\> tag, :article =\> article)

Does that help?

## Best,

Marnen Laibow-Koser  
removed\_email\_address@domain.invalid  
[http://www.marnen.org](http://www.marnen.org)

---

<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:** [January 2, 2009, 12:28am UTC](https://www.ruby-forum.com/t/habtm-3-table-relation/156029/3 "2009-01-02T00:28:08Z")

</div>

that worked thank you very much!

On Sat, Dec 20, 2008 at 2:51 AM, Marnen Laibow-Koser  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> > user.article \<\< article
> 
> belongs\_to :tag  
> Does that help?
> 
> ## Best,
> 
> Marnen Laibow-Koser  
> removed\_email\_address@domain.invalid  
> [http://www.marnen.org](http://www.marnen.org)
> 
> >

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