2012-09-12 49 views
0

我有这种多态关联,用户可以有很多评论,学校可以有很多评论,评论可以有很多评论(或者在我的命名案例回复中):找到多态评论模型中的评论的回复者

class Comment < ActiveRecord::Base 
    attr_accessible :content 
    has_many :replies, :as => :commentable, :class_name => "Comment" # replies to comments 

    belongs_to :commentable, :polymorphic => true 
    belongs_to :commentor, :class_name => "User", :foreign_key => "user_id" 
end 

class User < ActiveRecord::Base 
    has_many :comments, :as => :commentable 
    has_many :commentors, # all users who commented on a user 
    :through => :comments, 
    :source => :commentor 
end 

class School < ActiveRecord::Base 
    has_many :comments, :as => :commentable 
    has_many :commentors, # all users who commented on a school 
    :through => :comments, 
    :source => :commentor 
end 

在用户,我可以检索所有谁使用@user.commentors用户评论。学校也是如此,即@school.commentors

对于评论模型,我希望在评论模型中实现同样的功能,在那里我可以找到所有评论者(或者我猜测评论者)的评论;然而,我不知道自has_many后创建了什么样的关联:通过关联不会像用户和学校模型那样工作。

+0

工作的呢? has_many:reply_commentors,:through =>:reply,:source =>:commentor – weexpectedTHIS

+0

D'oh是的,这是有效的。哇,我很愚蠢。谢谢。 – Mavoir

+0

如果您想接受,我添加了我的评论作为答案。 – weexpectedTHIS

回答

0

使用此:

has_many :reply_commentors, :through => :replies, :source => :commentor