Rails Model has_many with multiple foreign_keys

前端 未结 8 1082
说谎
说谎 2020-11-28 06:04

Relatively new to rails and trying to model a very simple family \"tree\" with a single Person model that has a name, gender, father_id and mother_id (2 parents). Below is b

8条回答
  •  独厮守ぢ
    2020-11-28 06:09

    To improve on Kenzie's answer, you can achieve an ActiveRecord Relation by defining Person#children like:

    def children
       children_of_mother.merge(children_of_father)
    end
    

    see this answer for more details

提交回复
热议问题