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
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.
…
…
@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.
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