Hi, I have a modeling problem I can''t figure out yet. Say I have an club of members and every member can have favorite other members. class Member < ActiveRecord::Base has_many :favorites end class Favorite < ActiveRecord::Base belongs_to :member, :foreign_key => :member_id belongs_to :member, :foreign_key => :favorite_id end @tim = Member.create(:name => "Tim") @foo = Member.create(:name => "Foo") @joe = Member.new(:name => "Joe") @joe.favorites << Favorite.create(@tim) @joe.favorites << Favorite.create(@foo) @joe.save I guess that''s not right. I am obviously missing something. Any help appreciated -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
class Member... has_many :favorites has_many :favorite_members, :class_name => "Member", :through => :favorites end class Favorite ... belongs_to :member belongs_to :favorite_member, :class_name => "Member", :foreign_key => :favorite_id end ... ... @joe.favorite_members << @foo @joe.save That''s how I do it. Though I use friends and friendship, but it''s basically the same thing. On Jun 17, 9:55 am, Jens -- <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi, > > I have a modeling problem I can''t figure out yet. > > Say I have an club of members and every member can have > favorite other members. > > class Member < ActiveRecord::Base > has_many :favorites > end > > class Favorite < ActiveRecord::Base > belongs_to :member, :foreign_key => :member_id > belongs_to :member, :foreign_key => :favorite_id > end > > @tim = Member.create(:name => "Tim") > @foo = Member.create(:name => "Foo") > > @joe = Member.new(:name => "Joe") > @joe.favorites << Favorite.create(@tim) > @joe.favorites << Favorite.create(@foo) > @joe.save > > I guess that''s not right. I am obviously missing something. > Any help appreciated > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> > ... > ... > @joe.favorite_members << @foo > @joe.save > > That''s how I do it. Though I use friends and friendship, but it''s > basically the same thing.Thanks that was fast! I''ll give it a shot. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks to silkcom, that works pretty well. However I have to maintain an additional comment field in my favorite (join) model. How do I access that comment (read or update) in favorite? With this I can iterate over all favorites, however ... @joe.favorite_members.each do |f| end ... with f I''ll access Members, but I would like to access each comment in Favorites. Thanks again -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---