首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我被一个看似简单的active_record协会搞糊涂了

我被一个看似简单的active_record协会搞糊涂了
EN

Stack Overflow用户
提问于 2022-02-10 17:51:49
回答 1查看 35关注 0票数 1

我有两张桌子。一个包含家谱数据(家庭),另一个包含人数据。我想要创建一个关联,这样我就可以根据家谱表中的指针访问人员数据。例如:

代码语言:javascript
复制
    <% @families.each do |family| %>
...
    <td> <%= family.father.first_name %></td>

以下是压路机代码:

代码语言:javascript
复制
  def index
      @families = Family.joins(:people)
  end

以下是两个模式:

代码语言:javascript
复制
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);

以下是模型代码:

代码语言:javascript
复制
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

当我在网页上放个空档时,“家庭存在”,而相关的父亲却不存在。

代码语言:javascript
复制
>> @families[0].father
NameError: uninitialized constant Family::Father

我尝试了has_one的每一个组合,通过,foreign_key,源代码等等,我可以想到,没有任何东西产生一个“家庭”对象与一个链接(或相关)‘父亲’对象。

我做错了什么?

EN

回答 1

Stack Overflow用户

发布于 2022-02-10 21:04:39

在这里,我将假设我们只是对生物连接进行建模,而不是对真实世界中的整个蠕虫进行建模,我将坚持Rails的约定,而不是解开您的模式。

如果您想将父母组合成不同的列放在一个表上,那么您将这样做:

代码语言:javascript
复制
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
end
代码语言:javascript
复制
class Family < ApplicationRecord
  belongs_to :father, 
    class_name: 'Person'
  belongs_to :mother, 
    class_name: 'Person'
  has_many :children, 
    class_name: 'Person',
    inverse_of: :family
end
代码语言:javascript
复制
class 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

到目前为止很简单。然而,一旦你开始给一个人的孩子添加关系,它就会变得疯狂:

代码语言:javascript
复制
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中的一个外键。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71069965

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档