我有 Project has_many :locations
和 Locations has_many :comments
,以及 Project has_many :plans
和 Plan has_many :comments
。如何选择属于一个项目的所有可评论资源的所有评论?如果相关的话,我正在使用 ActiveAdmin
和 ActiveAdmin::Comment
。我试过的:
p = Project.first
comments = p.locations.map(&:comments).flatten + p.plans.map(&:comments).flatten
但是是否有 ActiveRecord 或 Rails 方法可以做到这一点?或者是否有可以完成所有这些的 SQL 连接?谢谢!
最佳答案
如果你通过位置和相同的计划进行 has_many location_comments 会更好。您也可以制作如下模型方法:
def all_comment
self.location_comments + self. plans_comments
end
关于sql - ActiveRecord 查找子资源的所有子资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20053808/