我有两张桌子。一个包含家谱数据(家庭),另一个包含人数据。我想要创建一个关联,这样我就可以根据家谱表中的指针访问人员数据。例如:
<% @families.each do |family| %>
...
<td> <%= family.father.first_name %></td>以下是压路机代码:
def index
@families = Family.joins(:people)
end以下是两个模式:
CREATE TABLE "people" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "name_prefix" varchar, "first_name" varchar, "middle_name" varchar, "last_name" varchar, "name_suffix" varchar, "date_of_birth" varchar, "date_of_death" varchar, "gender" varchar, "notes" varchar, "sync_outlook" varchar, "sync_phone" varchar, "flags" varchar, "created_at" datetime(6) NOT NULL, "updated_at" datetime(6) NOT NULL, "place_of_birth" varchar, "aliases" varchar);
CREATE TABLE "families" ("id" integer NOT NULL PRIMARY KEY, "fam_notes" varchar DEFAULT NULL, "fam_fatherid" integer DEFAULT NULL, "fam_motherid" integer DEFAULT NULL, "fam_weddingdate" varchar DEFAULT NULL, "fam_weddingplace" varchar DEFAULT NULL, "created_at" datetime(6) NOT NULL, "updated_at" datetime(6) NOT NULL);以下是模型代码:
class Family < ApplicationRecord
has_many :childlinks, foreign_key: "child_family"
has_many :people, through: :childlinks
#has_one :father, through: :families, source: "fam_fatherid"
has_one :father, foreign_key: "fam_fatherid"
has_one :person, through: :father
end当我在网页上放个空档时,“家庭存在”,而相关的父亲却不存在。
>> @families[0].father
NameError: uninitialized constant Family::Father我尝试了has_one的每一个组合,通过,foreign_key,源代码等等,我可以想到,没有任何东西产生一个“家庭”对象与一个链接(或相关)‘父亲’对象。
我做错了什么?
发布于 2022-02-10 21:04:39
在这里,我将假设我们只是对生物连接进行建模,而不是对真实世界中的整个蠕虫进行建模,我将坚持Rails的约定,而不是解开您的模式。
如果您想将父母组合成不同的列放在一个表上,那么您将这样做:
class CreateFamilies < ActiveRecord::Migration[6.1]
def change
create_table :families do |t|
t.belongs_to :father,
null: false,
foreign_key: { to_table: :persons }
t.belongs_to :mother,
null: false,
foreign_key: { to_table: :persons }
t.timestamps
end
end
endclass Family < ApplicationRecord
belongs_to :father,
class_name: 'Person'
belongs_to :mother,
class_name: 'Person'
has_many :children,
class_name: 'Person',
inverse_of: :family
endclass Person < ApplicationRecord
# @todo add a family_id foreign key column to persons
# this is a persons link to their parents
# Must be nullable to avoid a chicken-vs-egg scenario
belongs_to :family,
optional: true,
inverse_of: :children
has_one :father, through: :family
has_one :mother, through: :family
end请注意,您实际上不需要孩子和家庭之间的连接表,因为关系商店是一对多的,而不是多到多的。每个孩子只能属于一组父母。当外键在此模型表上时,还需要使用belongs_to。
到目前为止很简单。然而,一旦你开始给一个人的孩子添加关系,它就会变得疯狂:
class Person < ApplicationRecord
# this is a persons link to their parents
# Must be nullable to avoid a chicken-vs-egg scenario
belongs_to :family,
optional: true,
inverse_of: :children
has_one :father, through: :family
has_one :mother, through: :family
has_many :maternal_families,
class_name: 'Family',
foreign_key: :mother_id
has_many :maternal_children,
through: :maternal_families,
source: :children
has_many :paternal_families,
class_name: 'Family',
foreign_key: :father_id
has_many :paternal_children,
through: :paternal_families,
source: :children
end您需要这么多查询的原因是,每个has_many定位都必须指向ActiveRecord中的一个外键。
https://stackoverflow.com/questions/71069965
复制相似问题