2011-08-24 71 views
0

複雜查詢我有這個DB模式:使用Rails 3.X

    |-- comment 
     |-- Post -|-- comment 
     | 
     |   |-- comment 
User --|-- Post -|-- comment 
     | 
     |   |-- comment 
     |-- Post -|-- comment 

什麼是檢索屬於一個用戶的所有意見的最佳方式?

感謝

回答

4

您能給我們一個has_many :through關聯。

class User 
    has_many :posts 
    has_many :comments, :through => :posts 
end 

class Post 
    has_many :comments 
    belongs_to :user 
end 

class Comment 
    belongs_to :post 
end 

有了這個結構,你可以簡單地做:

user.comments