【发布时间】:2018-02-20 02:28:28
【问题描述】:
我有一个名为“Person”的模型,它有“mother”和“father”字段(一对一的关系指向同一个类),所以现在我想获取一个人的所有孩子 我该怎么做?
这是模型
class Person < ApplicationRecord
has_many :mails, dependent: :destroy
has_many :phones, dependent: :destroy
has_one :spouse, class_name: 'Person'
has_one :father, class_name: 'Person'
has_one :mother, class_name: 'Person'
end
这就是迁移
class CreatePeople < ActiveRecord::Migration[5.1]
def change
create_table :people do |t|
t.string :rut, unique: true
t.string :dv
t.string :names
t.string :paternal_surname
t.string :maternal_surname
t.date :birth_date
t.date :death_date
t.string :civil_status
t.string :sex
t.string :nationality
t.references :father
t.references :mother
t.references :spouse
t.timestamps
end
end
end
我想通过以下方式访问孩子:
Person.children
但我是 RoR 的新手,我不知道该怎么做。
【问题讨论】:
标签: ruby-on-rails