I’m playing around with RoR and I’m having trouble figuring out how to
describe a certain relationship. I’m building a social networking site
where you have users who can comment on other users, photos, videos, and
blogs. I’ve figured out how to have a comment belong to a user or media
item through a polymorphic relationships but I dont’ know how to have
the comment also be “owned” by the user who made the comment. I’ve
included some code to help with the description:
#User class
class User < ActiveRecord::Base
has_many :photos
has_many :blogs
…
end
#photo (also blog and video are similar)
class Photo < ActiveRecord::Base
belongs_to :user
has_many :comments, :as => :commentable
end
#comment class
class Comment < ActiveRecord::Base
belongs_to :commentable, :polymorphic => true
end
#comment table includes the following fields:
id
content <— the actual comment text
user_id <— the user who made the comment
commentable_id
commentable_type
I’ve thought this over for 2 days and I still can’t seem to figure out a
way to describe what I want to describe… Does anyone have any ideas or
suggestions on how to do this?