ruby on rails - Multiple polymorphic relations of same type in one model - MongoID -
i couldn't figure out how work title correctly, let me explain.
i have class like:
class include mongoid::document belongs_to :likable, polymorphic: true belongs_to :user end class submission include mongoid::document belongs_to :creator, class_name: 'user', inverse_of: :submissions has_many :likes, :as => :likable, :dependent => :destroy end
then class causes problem:
class user include mongoid::document has_many :submissions, :dependent => :destroy has_many :liked_submissions, :as => :likable, :dependent => :destroy, class_name: 'like' has_many :liked_comments, :as => :likable, :dependent => :destroy, class_name: 'like' end
now, problem can't figure out how have both 'liked_comments' , 'liked_comments' in user class. when have above error: ambiguous relations :liked_submissions, :liked_comments defined on user.
when trying create submission.
i looked around, , found github issue said may not possible, can't find issue again, , wasn't sure related issue.
is possible, , if not, suggestions getting around it? use likes in blogpost model well, if doesn't work, i'll have find way.
thanks in advance.
i tried , works using mongoid version 4.0.2:
class include mongoid::document belongs_to :likable, polymorphic: true belongs_to :user, inverse_of: :user end class submission include mongoid::document belongs_to :creator, class_name: 'user', inverse_of: :submissions has_many :likes, as: :likable, dependent: :destroy end class user include mongoid::document has_many :submissions, dependent: :destroy has_many :likes, as: :user has_many :liked_submissions, as: :likable, dependent: :destroy, class_name: 'like' has_many :liked_comments, as: :likable, dependent: :destroy, class_name: 'like' end class blogpost include mongoid::document has_many :likes, as: :likable, dependent: :destroy end
also user.liked_submissions
, user.liked_comments
same relation equivalent declaring alias this:
class user include mongoid::document has_many :submissions, dependent: :destroy has_many :likes, as: :user has_many :liked_submissions, as: :likable, dependent: :destroy, class_name: 'like' alias :liked_comments :liked_submissions end
Comments
Post a Comment