Ruby on rails 使用Rails 3.X进行复杂查询
我有一个db模式:Ruby on rails 使用Rails 3.X进行复杂查询,ruby-on-rails,database,Ruby On Rails,Database,我有一个db模式: |-- comment |-- Post -|-- comment | | |-- comment User --|-- Post -|-- comment | | |-- comment |-- Post -|-- comment 检索属于一个用户的所有注释的最佳方法是什么 谢谢您可以为我们建立一个协会 class User
|-- comment
|-- Post -|-- comment
|
| |-- comment
User --|-- Post -|-- comment
|
| |-- comment
|-- Post -|-- comment
检索属于一个用户的所有注释的最佳方法是什么
谢谢您可以为我们建立一个协会
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
你可以给我们一个协会
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