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 -- Posted via http://www.ruby-forum.com/.
Jens Krahe 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 Taylor wrote:> Jens Krahe 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-- Posted via http://www.ruby-forum.com/.
Jens Krahe 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
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-- Posted via http://www.ruby-forum.com/.
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? -- Posted via http://www.ruby-forum.com/.
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. On 3/8/06, Jens Krahe <jens.krahe@gmail.com> wrote:> > 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? > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060308/ddc7ed3e/attachment-0001.html
Brian Donahue 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 -- Posted via http://www.ruby-forum.com/.