# M:n or multiple 1:n?

**URL:** <https://www.ruby-forum.com/t/m-n-or-multiple-1-n/54514>\
**Category:** Rails\
**Created:** [March 7, 2006, 2:43pm UTC](https://www.ruby-forum.com/t/m-n-or-multiple-1-n/54514 "2006-03-07T14:43:10Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![krahe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/df705f/32.png) [@krahe](https://www.ruby-forum.com/u/krahe)\
**Post date:** [March 7, 2006, 2:43pm UTC](https://www.ruby-forum.com/t/m-n-or-multiple-1-n/54514/1 "2006-03-07T14:43:10Z")

</div>

I have a Newbee question:

i have three tables and want to connect them. so is it stupid to make a  
triple m:n (rails style xs\_ys\_zs) or do i have to make a new table  
(newtable) where i got multiple 1:n?

the habtm (has and belongs to many) do only work proper to join two  
tables, or i am wrong?

-jens

---

<div class="post-metadata">

**Author:** ![krahe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/df705f/32.png) [@krahe](https://www.ruby-forum.com/u/krahe)\
**Post date:** [March 7, 2006, 2:54pm UTC](https://www.ruby-forum.com/t/m-n-or-multiple-1-n/54514/2 "2006-03-07T14:54:59Z")

</div>

Jens K. wrote:

> i have three tables and want to connect them. so is it stupid to make a  
> triple m:n (rails style xs\_ys\_zs) or do i have to make a new table  
> (newtable) where i got multiple 1:n?

It depends on whether you want the join table to have attributes. If you  
do, or think you might in the future, then you need a join model (with  
the appropriate table). It’s a little messier, but has\_many :through  
coming in Rails 1.1 makes life easier.

Otherwise, you can get away with HABTM.

> the habtm (has and belongs to many) do only work proper to join two  
> tables, or i am wrong?

Yes, only two.

Cheers!

Tom

---

<div class="post-metadata">

**Author:** ![krahe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/df705f/32.png) [@krahe](https://www.ruby-forum.com/u/krahe)\
**Post date:** [March 7, 2006, 3:32pm UTC](https://www.ruby-forum.com/t/m-n-or-multiple-1-n/54514/3 "2006-03-07T15:32:33Z")

</div>

Tom T. wrote:

> Jens K. wrote:
> 
> > i have three tables and want to connect them. so is it stupid to make a  
> > triple m:n (rails style xs\_ys\_zs) or do i have to make a new table  
> > (newtable) where i got multiple 1:n?
> 
> It depends on whether you want the join table to have attributes. If you  
> do, or think you might in the future, then you need a join model (with  
> the appropriate table). It’s a little messier, but has\_many :through  
> coming in Rails 1.1 makes life easier.
> 
> Otherwise, you can get away with HABTM.

how i can read attributes stored in the join table?

M:N Table  
user\_id  
link\_id  
text \<- how i can read out this value  
updated\_at \<- how i can read out this value  
created\_at \<- how i can read out this value

is it user.text or link.text?

> > the habtm (has and belongs to many) do only work proper to join two  
> > tables, or i am wrong?
> 
> Yes, only two.
> 
> Cheers!
> 
> Tom

---

<div class="post-metadata">

**Author:** ![krahe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/df705f/32.png) [@krahe](https://www.ruby-forum.com/u/krahe)\
**Post date:** [March 7, 2006, 4:39pm UTC](https://www.ruby-forum.com/t/m-n-or-multiple-1-n/54514/4 "2006-03-07T16:39:28Z")

</div>

Jens K. wrote:

> how i can read attributes stored in the join table?
> 
> M:N Table  
> user\_id  
> link\_id  
> text \<- how i can read out this value  
> updated\_at \<- how i can read out this value  
> created\_at \<- how i can read out this value
> 
> is it user.text or link.text?

Just access it as a normal model.

@a = JoinModel.find :first  
@a.text

You can also access it through the models it belongs to, such as  
user.join\_models…

Cheers!

Tom

---

<div class="post-metadata">

**Author:** ![krahe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/df705f/32.png) [@krahe](https://www.ruby-forum.com/u/krahe)\
**Post date:** [March 8, 2006, 5:47pm UTC](https://www.ruby-forum.com/t/m-n-or-multiple-1-n/54514/5 "2006-03-08T17:47:45Z")

</div>

I do not understand it.

I have installed the login\_engine plugin. There I put the  
has\_and\_belongs\_to\_many :links

In the link model I put the has\_and\_belongs\_to\_many :users

Rails do not recognize the links\_users table.

Why?

---

<div class="post-metadata">

**Author:** ![krahe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/df705f/32.png) [@krahe](https://www.ruby-forum.com/u/krahe)\
**Post date:** [March 7, 2006, 9:15pm UTC](https://www.ruby-forum.com/t/m-n-or-multiple-1-n/54514/6 "2006-03-07T21:15:17Z")

</div>

That’s that easy? whow, i will test it tomorrow!

cheers!  
-Jens

> Just access it as a normal model.
> 
> @a = JoinModel.find :first  
> @a.text
> 
> You can also access it through the models it belongs to, such as  
> user.join\_models…
> 
> Cheers!
> 
> Tom

---

<div class="post-metadata">

**Author:** ![krahe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/df705f/32.png) [@krahe](https://www.ruby-forum.com/u/krahe)\
**Post date:** [March 8, 2006, 7:12pm UTC](https://www.ruby-forum.com/t/m-n-or-multiple-1-n/54514/7 "2006-03-08T19:12:04Z")

</div>

Jens, I recently switched from login\_engine to the acts\_as\_authenticated  
plugin based on this article:  
[http://glu.ttono.us/articles/2006/02/06/rails-best-practices-tips-and-tricks](http://glu.ttono.us/articles/2006/02/06/rails-best-practices-tips-and-tricks)

and my experiences with login\_engine. You might want to consider  
acts\_as\_authenticated.

---

<div class="post-metadata">

**Author:** ![krahe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/df705f/32.png) [@krahe](https://www.ruby-forum.com/u/krahe)\
**Post date:** [March 8, 2006, 9:55pm UTC](https://www.ruby-forum.com/t/m-n-or-multiple-1-n/54514/8 "2006-03-08T21:55:57Z")

</div>

Brian D. wrote:

> Jens, I recently switched from login\_engine to the acts\_as\_authenticated  
> plugin based on this article:  
> [http://glu.ttono.us/articles/2006/02/06/rails-best-practices-tips-and-tricks](http://glu.ttono.us/articles/2006/02/06/rails-best-practices-tips-and-tricks)
> 
> and my experiences with login\_engine. You might want to consider  
> acts\_as\_authenticated.

Thanks Brian,

I currently go away from M:N. I will try acts\_as\_authenticated. So will  
do multiple 1:N on one table like David Heinemeier wrote  
([http://article.gmane.org/gmane.comp.lang.ruby.rails/32742/](http://article.gmane.org/gmane.comp.lang.ruby.rails/32742/)). So I can  
use :though when Rails 1.1 comes up.

Rails is really great, but when a complex data model is on duty it’s  
anything than easy…

when you want to store M:N’s you have to add this on scaffolding

def create  
@link = Link.new(params[:link])  
if @link.save  
@link.users = User.find\_by\_ **Something**  
@link.update\_attributes(params[:link])  
flash[:notice] = ‘Link was successfully created.’  
redirect\_to :action =\> ‘list’  
else  
render :action =\> ‘new’  
end  
end
