How do I find a child given two parent_ids in Rails?
I am not entirely sure how to word this question, but here goes:
I am trying to find all entries in a table given two parent variables.
So,
father has_many :children
mother has_many :children
child belongs_to :father
child belongs_to :mother
Now, how might I find all children based on a specific father and mother?
If I left out important information,开发者_如何学运维 please let me know. Otherwise, I greatly appreciate any help in figuring this out.
Use this:
Child.find_all_by_father_id_and_mother_id(father.id, mother_id)
Here are a few ways:
@children = Child.where(:mother_id => mother.id, :father_id => father.id).all
or if you've already loaded the children:
@children = mother.children & father.children
精彩评论