2012-09-12 3 views
0

사용자가 많은 주석을 가질 수 있고, 학교가 많은 주석을 가질 수 있고, 주석이 많은 주석을 가질 수있는 (또는 내 명명 케이스 응답에서)이 다형성 연관이 있습니다. 사용자에 다형성 주석 모델의 주석에 대한 replitter 찾기

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 : association을 통해 사용자 및 학교 모델에서 작동하는 방식과 같이 작동하지 않으므로 어떤 종류의 연관성을 만들지 모릅니다.

+0

이 일을합니까? has_many : reply_commentors, : through => : 답장, : source => : commentor – weexpectedTHIS

+0

그래, 맞아. 와우, 나는 어리 석다. 감사. – Mavoir

+0

동의하려는 경우 내 의견을 답으로 추가했습니다. – weexpectedTHIS

답변

0

사용이 :

has_many :reply_commentors, :through => :replies, :source => :commentor 
관련 문제