I have what I think is a fairly complex modeling problem for you guys.
So I have a user class. There are three link type classes: picture, video, and story. A user can vote, bookmark, and add any of those types. Essentially I want to be able to say: user.stories and get the stories the user has uploaded user.votes.stories and get the votes that are stories user.votes.videos and get the votes that are videos user.bookmarks.videos and get the bookmarks for the videos
So these are essentially join tables, but I can't seem to get them to go properly, have tried polymorphism and has_many_polymorphs, limited success, limited knowledge.
Now I also want to be able to go the other way:
story.votes story.user story.bookmarks
Well a user has_many :videos (and the story :belongs_to :user) which
takes care of who uploaded what
A user has_many votes, has_many bookmarks, again that's not the
complicated bit. From now on lets just think about bookmarks since
it's basically the same think at this point.
The bookmark model could belong_to :user, and belongs_to :
bookmark_content, :polymorphic => true
Over on video, we can say video has_many :bookmarks, :as =>
bookmark_content
This takes care of video.bookmarks.
The only remaining bit is user.bookmarks.videos
off the top of my head you could solve it like this: replace the
has_many :bookmarks on user with
has_many :bookmarks do
def videos
Video.find :all, :joins => :bookmarks, :conditions =>
['bookmarks.user_id = ?', proxy_owner.id]
end
end
you can define bookmarks.stories, bookmarks.pictures in a similar way
Fred