Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 3 多个的ActiveRecord查询具有多个关联_Ruby On Rails 3_Activerecord - Fatal编程技术网

Ruby on rails 3 多个的ActiveRecord查询具有多个关联

Ruby on rails 3 多个的ActiveRecord查询具有多个关联,ruby-on-rails-3,activerecord,Ruby On Rails 3,Activerecord,使用Rails 3.2,我有以下型号: class Category < ActiveRecord::Base has_many: posts end class Post < ActiveRecord::Base belongs_to :category has_many :comments end class Comment < ActiveRecord::Base belongs_to :post end 类别

使用Rails 3.2,我有以下型号:

class Category < ActiveRecord::Base
  has_many: posts
end

class Post < ActiveRecord::Base
  belongs_to :category
  has_many :comments
end

class Comment < ActiveRecord::Base
  belongs_to :post
end
类别

我现在正在寻找一个查询,以查找属于某个类别的所有注释。

我将执行如下连接查询:

Comment.joins(:post=>:category)。其中(“categories.id=?”,category)


需要注意的一点是内存使用情况。如果你在评论、帖子和分类中有很多字段,还有很多记录,那就不好看了。因此,请使用
选择
来指定所需的字段。

感谢您的快速回复!我错过了.joins(:post=>:category)部分,只使用了.joins(:post).Done(stackoverflow只允许在几分钟后接受答案)。哦,是的:)谢谢!