如何在Rails中找到给出两个parent_id的孩子?

时间:2011-07-18 05:46:55

标签: ruby-on-rails ruby parent-child

我不完全确定如何说出这个问题,但这里有:

我试图在给定两个父变量的表中找到所有条目。

所以,

father has_many :children
mother has_many :children
child belongs_to :father
child belongs_to :mother

现在,我如何根据特定的父亲和母亲找到所有孩子?

如果我遗漏了重要信息,请告诉我。否则,我非常感谢你解决这个问题。

2 个答案:

答案 0 :(得分:1)

使用此:

Child.find_all_by_father_id_and_mother_id(father.id, mother_id)

答案 1 :(得分:1)

以下是一些方法:

@children = Child.where(:mother_id => mother.id, :father_id => father.id).all

或者如果您已经加载了孩子:

@children = mother.children & father.children