Forum: Ruby on Rails m:n or multiple 1:n?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Jens K. (Guest)
on 2006-03-07 15:43
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
Tom T. (Guest)
on 2006-03-07 15:54
(Received via mailing list)
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
Jens K. (Guest)
on 2006-03-07 16:32
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
Tom T. (Guest)
on 2006-03-07 17:39
(Received via mailing list)
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
Jens K. (Guest)
on 2006-03-07 22:15
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 K. (Guest)
on 2006-03-08 18:47
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?
Brian D. (Guest)
on 2006-03-08 20:12
(Received via mailing list)
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...

and my experiences with login_engine.  You might want to consider
acts_as_authenticated.
Jens K. (Guest)
on 2006-03-08 22:55
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...
>
> 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
This topic is locked and can not be replied to.