# Multiple HABTM

**URL:** <https://www.ruby-forum.com/t/multiple-habtm/119200>\
**Category:** Rails\
**Created:** [November 4, 2007, 10:00pm UTC](https://www.ruby-forum.com/t/multiple-habtm/119200 "2007-11-04T22:00:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![heldopslippers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b9bd4f/32.png) [@heldopslippers](https://www.ruby-forum.com/u/heldopslippers)\
**Post date:** [November 4, 2007, 10:00pm UTC](https://www.ruby-forum.com/t/multiple-habtm/119200/1 "2007-11-04T22:00:28Z")

</div>

hee guys…

I have a problem with HABTM…  
I have articles en users… wich are connected to rights… beceause  
users can have more then 1 right and rights can have more than one  
user… and the same by articles of course

I have 3 models:

the user: has\_and\_belongs\_to\_many :rights  
the article: has\_and\_belongs\_to\_many :rights

and rights: has\_and\_belongs\_to\_many :users  
has\_and\_belongs\_to\_many :articles

and 2 join tables.

I have a user\_id and I want to list all the articles which he has access  
to:  
@user = User.find\_by\_id(2)  
@right = @user.rights  
@article\_list= @right.articles

but this doesn’t work… Does anyone now how to get multiple articles  
from one user ?

---

<div class="post-metadata">

**Author:** ![heldopslippers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b9bd4f/32.png) [@heldopslippers](https://www.ruby-forum.com/u/heldopslippers)\
**Post date:** [November 5, 2007, 2:57pm UTC](https://www.ruby-forum.com/t/multiple-habtm/119200/2 "2007-11-05T14:57:54Z")

</div>

Hello,

You are actually getting a array of rights from this:  
@right = @user.rights

So not a right per se. You should call @right.articles on a Right  
object…

Julien.

On Nov 4, 4:00 pm, Heldop S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![heldopslippers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b9bd4f/32.png) [@heldopslippers](https://www.ruby-forum.com/u/heldopslippers)\
**Post date:** [November 5, 2007, 5:35pm UTC](https://www.ruby-forum.com/t/multiple-habtm/119200/3 "2007-11-05T17:35:06Z")

</div>

removed\_email\_address@domain.invalid wrote:

> Hello,
> 
> You are actually getting a array of rights from this:  
> @right = @user.rights
> 
> So not a right per se. You should call @right.articles on a Right  
> object…
> 
> Julien.
> 
> On Nov 4, 4:00 pm, Heldop S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Thanxs for the response Julien !!  
Hmmm I think I dont understand it quit well …

is tryed a few things…

@right = User.find\_by\_id(1)  
@article\_list= @right.articles

and tryed:

@right = User.find\_by\_id(1)  
@article = @right.articles  
@article\_list= @articles.articles

and:

@right = User.find\_by\_id(1)  
@article\_list = @right.articles.rights

but it all doens’t work ☹  
I hope someone can tell wat I do wrong…

---

<div class="post-metadata">

**Author:** ![heldopslippers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b9bd4f/32.png) [@heldopslippers](https://www.ruby-forum.com/u/heldopslippers)\
**Post date:** [November 5, 2007, 6:01pm UTC](https://www.ruby-forum.com/t/multiple-habtm/119200/4 "2007-11-05T18:01:35Z")

</div>

Heldop S. wrote:

> removed\_email\_address@domain.invalid wrote:
> 
> > Hello,
> > 
> > You are actually getting a array of rights from this:  
> > @right = @user.rights
> > 
> > So not a right per se. You should call @right.articles on a Right  
> > object…
> > 
> > Julien.
> > 
> > On Nov 4, 4:00 pm, Heldop S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
> 
> Thanxs for the response Julien !!  
> Hmmm I think I dont understand it quit well …
> 
> is tryed a few things…
> 
> @right = User.find\_by\_id(1)  
> @article\_list= @right.articles
> 
> and tryed:
> 
> @right = User.find\_by\_id(1)  
> @article = @right.articles  
> @article\_list= @articles.articles
> 
> and:
> 
> @right = User.find\_by\_id(1)  
> @article\_list = @right.articles.rights
> 
> but it all doens’t work ☹  
> I hope someone can tell wat I do wrong…

a little update…  
If i say that right\_id is 1 than it does work…

@right = Right.find(1)  
@article\_list = @right.articles

but if I do:

@right = Right.find(:all)  
@article\_list = @right.articles

so I want to select all the right ID’s and have there the articles from  
it is comming with an error: undefined method `articles’

I hope someone can help me
