Rails Model has_many with multiple foreign_keys

前端 未结 8 1083
说谎
说谎 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:15

    I believe you can achieve the relationships you want using :has_one.

    class Person < ActiveRecord::Base
      has_one :father, :class_name => 'Person', :foreign_key => 'father_id'
      has_one :mother, :class_name => 'Person', :foreign_key => 'mother_id'
      has_many :children, :class_name => 'Person'
    end
    

    I'll confirm and edit this answer after work ; )

提交回复
热议问题