I’m using polymorphic association on my Review model for User and Post.
However i would also make sense to have a owner for each review.
BY owner i mean a associated user model. But this bring problems on the table since users can review and has reviews from other users
What is the best way to go about. Any ideas of solutions are welcome.
Review.rb
belongs_to :reviewable, polymorphic: true
belongs_to :user # owner of the reviews
User.rb
# reviews from others
has_many :reviews, as: :reviewable, dependent: :destroy
has_many :reviews # user written reviews
Post.rb
has_many :reviews, as: :reviewable, dependent: :destroy
The obvious problem is that you have two associations in your
Usermodel with the same name. One simple fix is to rename one of them. Since yourPostalready has areviewsassociation that matches a similarreviewspolymorphic association, you could rename the user written reviews to be something like this:By specifying the class name, Rails can then infer the rest of the information and get you the correct
Reviewobjects whereuser_idmatches on yourUser.