我有一个模型Parent
有很多孩子Child
。我想要获得所有父级模型,并显示父级的每个Child
。据我所知,这是Rails的includes
方法的经典用例。使用Rails包括对儿童的条件
但是,我无法让Rails向子模型添加条件,而不将父模型限制为有子模型的子模型。
例如,这只是输出父母有孩子:
Parent.includes(:children).where(children: {age: 10}).each do |parent|
# output parent info
parent.children.where("age = 10").each do |child|
#output child info
end
end
我看了Rails includes with conditions但似乎我有同样的问题作为问题的任择议定书和接受的答案既不一部分并没有解决它(它只有一些父母,或采用多个查询)。
什么数据库管理系统使用的是? –
我使用MySQL – you786