Suppose I have a User, Post and a Comment model, that allow a user to comment on a post. I’ve set up a polymorphic relationship like follows:
class User < ActiveRecord::Base
has_many :comments
end
class GroupRun < ActiveRecord::Base
has_many :comments, :as => :commentable
end
class Comment < ActiveRecord::Base
belongs_to :commentable, polymorphic: true
belongs_to :user
end
I’d now like to allow a user to comment on another user, but am running into difficulty. I’d have thought I could do something along the lines of has_many :notes, :as_commentable but this doesn’t work.
Any advice?
You have to add this to your User model:
I know the name of the relation is awful but it is easily understandable and you can change it as you want.
So you should have this after: