M:n or multiple 1:n?

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

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

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

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

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?

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

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

and my experiences with login_engine. You might want to consider
acts_as_authenticated.

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

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/). 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