Some help please!! class Article < ActiveRecord::Base has_and_belongs_to_many :tags, :join_table => :articles_tags_users has_and_belongs_to_many :users, :join_table => :articles_tags_users end class Tag < ActiveRecord::Base has_and_belongs_to_many :articles, :join_table => :articles_tags_users has_and_belongs_to_many :users, :join_table => :articles_tags_users end class User < ActiveRecord::Base has_and_belongs_to_many :articles, :join_table => :articles_tags_users has_and_belongs_to_many :tags, :join_table => :articles_tags_users end then in the controller user = User.new tag = Tag.find(id) article = Article.find(id) user.tag << tag user.article << article all this creates 2 tuples on the database articles_tags_users instead of one with all the values Do somebody knows how to do this? Thanks ! -- Felipe Vergara Contesse Ingeniería Civil Industrial UC --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Dec 19, 8:26 am, "Felipe Vergara" <felverg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Some help please!! > class Article < ActiveRecord::Base > has_and_belongs_to_many :tags, :join_table => :articles_tags_users[...]> user = User.new > tag = Tag.find(id) > article = Article.find(id) > user.tag << tag > user.article << article > > all this creates 2 tuples on the database articles_tags_users instead of one > with all the values > > Do somebody knows how to do this?I hope someone will correct me if I''m wrong, but I believe you will need to use has_many :through for this case -- HABTM isn''t meant for 3- way joins as far as I know. You''ll need something like this: class Article < ActiveRecord::Base has_many :correlations has_many :tags, :through => :correlations has_many :users, :through => :correlations end (similarly for User and Tag) class Correlation < ActiveRecord::Base belongs_to :article belongs_to :tag belongs_to :user end Then your sample controller code would become something like user = User.new tag = Tag.find(id) article = Article.find(id) user.correlations << Correlation.new(:tag => tag, :article => article) Does that help? Best, -- Marnen Laibow-Koser marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org http://www.marnen.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
that worked thank you very much! On Sat, Dec 20, 2008 at 2:51 AM, Marnen Laibow-Koser <marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org>wrote:> > On Dec 19, 8:26 am, "Felipe Vergara" <felverg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Some help please!! > > class Article < ActiveRecord::Base > > has_and_belongs_to_many :tags, :join_table => :articles_tags_users > [...] > > user = User.new > > tag = Tag.find(id) > > article = Article.find(id) > > user.tag << tag > > user.article << article > > > > all this creates 2 tuples on the database articles_tags_users instead of > one > > with all the values > > > > Do somebody knows how to do this? > > I hope someone will correct me if I''m wrong, but I believe you will > need to use has_many :through for this case -- HABTM isn''t meant for 3- > way joins as far as I know. You''ll need something like this: > > class Article < ActiveRecord::Base > has_many :correlations > has_many :tags, :through => :correlations > has_many :users, :through => :correlations > end > > (similarly for User and Tag) > > class Correlation < ActiveRecord::Base > belongs_to :article > belongs_to :tag > belongs_to :user > end > > Then your sample controller code would become something like > > user = User.new > tag = Tag.find(id) > article = Article.find(id) > user.correlations << Correlation.new(:tag => tag, :article => article) > > Does that help? > > Best, > -- > Marnen Laibow-Koser > marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > http://www.marnen.org > > >-- Felipe Vergara Contesse Ingeniería Civil Industrial UC --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---