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