3

2つのモデル(parentchild)があり、親にhas_manyの子があり、親の配列があり、それらすべての親のすべての子を取得したい場合、SQLステートメントを手動で記述せずにRailsでこれを行う方法はありますか? ?

これが私がやりたいことです:

@parents = Parent.where("[various conditions]")
@children = @parents.children
4

1 に答える 1

7
Child.where(:parent_id => @parents.pluck(:id))

また

@parent_ids = Parent.where("[various conditions]").pluck(:id)
Child.where(:parent_id => @parent_ids}

または、結合を使用できます

Child.join(:parent).merge(Parent.where("[various conditions]")) #!!readonly
于 2012-05-20T00:57:39.210 に答える