Rails 6.0关联查询

我正在Rails 6.0上使用ruby。 我有两个实体,一个事件表和一个gem模块,例如为人(人)。事件表中的字段之一是contact_person,该字段应指向gem模块people(人)。当我尝试用种子数据填充表格时,我得到: activeRecord :: RecordInvalid:验证失败:联系人必须存在

在下面您可以找到数据库定义。我在这里很新,是否暗示我做错了什么? 谢谢您的协助。

关于, 丹妮

迁移事件表文件:

class CreateApplicationEvents < activeRecord::Migration[6.0]
  def change
        create_table :events
                t.references :contact_person,null: true,foreign_key: { 
                    to_table: :core_people_and_companies_people }
                t.timestamps
        end
  end
end

事件的模型文件:

class Event < ApplicationRecord
  belongs_to :contact_person,class_name: '::CoreModels::PeopleAndCompanies::Person'
end

gem模块:

module CoreModels
    module PeopleAndCompanies
      Person.class_eval do
         has_many :events,:class_name => 'Event',:as => :contact_person
      end
    end
end
xuzhihua131792 回答:Rails 6.0关联查询

我已通过在事件的模型文件中添加-“可选:true”来解决此问题:

“属于:contact_person,类别名称:':: CoreModels :: PeopleAndCompanies :: Person',             可选:true”

谢谢大家。 此致,丹妮

本文链接:https://www.f2er.com/3151048.html

大家都在问