2012-03-22 117 views
0

我正在使用Rails 3.2。如何獲得博客的評論者

我有以下型號:

Blog 
Comment 
User 

class Blog < ActiveRecord::Base 
    has_many :comments 
end 

我要評論者對給定的博客列表。

我想是這樣

class Blog < ActiveRecord::Base 
     has_many :comments 
     has_many :commenters, ...fill in the blank... 
    end 

@blog.commenters應該返回用戶實例的數組。

我應該如何填寫上面的空白。

回答

1

我猜你有以下

class User 
    has_many :comments 
end 

class Comment 
    belongs_to :blog 
    belongs_to :user 
end 

class Blog 
    has_many :comments 
end 

所有你需要補充的是

class Blog 
    has_many :commenters, :through => :comments, :source => :user 
end 

注:需要在:source因爲comments的關係不叫commenters