Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 创建与多态嵌套属性的关联时出错_Ruby On Rails_Ruby_Ruby On Rails 5 - Fatal编程技术网

Ruby on rails 创建与多态嵌套属性的关联时出错

Ruby on rails 创建与多态嵌套属性的关联时出错,ruby-on-rails,ruby,ruby-on-rails-5,Ruby On Rails,Ruby,Ruby On Rails 5,我有三种型号客户,地址和信用备忘录 最初,我有一个非多态模型Address,当我创建CreditMemo时,我将其更改为多态模型,以便它们可以共享同一个模型。从那时起,当我尝试创建一个与客户不同的父类型的记录时,我得到一个验证错误,说客户父类型不存在 在我创建CreditMemo之前,我只是在Customer和Address之间有一个1对多关联。下面是我将地址更改为多态的迁移 class MakeAddressPolymorphic < ActiveRecord::Migration[5.

我有三种型号<代码>客户,
地址
信用备忘录

最初,我有一个非多态模型
Address
,当我创建
CreditMemo
时,我将其更改为多态模型,以便它们可以共享同一个模型。从那时起,当我尝试创建一个与
客户
不同的父类型的记录时,我得到一个验证错误,说客户父类型不存在

在我创建
CreditMemo
之前,我只是在
Customer
Address
之间有一个
1对多
关联。下面是我将
地址
更改为多态的迁移

class MakeAddressPolymorphic < ActiveRecord::Migration[5.1]
  def up
    rename_column :addresses, :customer_id, :addressable_id
    add_column :addresses, :addressable_type, :string
    add_index :addresses, [:addressable_id, :addressable_type]
    Address.reset_column_information
    Address.update_all(:addressable_type => "Customer")
  end

  def down
    rename_column :addresses, :addressable_id, :customer_id
    remove_column :addresses, :addressable_type
  end
end
我可以创建一个具有嵌套属性的客户。但是,当我尝试创建一个带有嵌套地址属性的
CreditMemo
时,出现以下错误:

PG::ForeignKeyViolation: ERROR: insert or update on table "addresses" violates foreign key constraint "fk_rails_d5f9efddd3" DETAIL: Key (addressable_id)=(36) is not present in table "customers". : INSERT INTO "addresses" ("line_1", "line_2", "city", "state", "zip_code", "address_type", "addressable_id", "created_at", "updated_at", "addressable_type") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING "id"
这是我的
客户
型号:

has_many :addresses, :as => :addressable, inverse_of: :addressable
accepts_nested_attributes_for :addresses
has_many :addresses, :as => :addressable, inverse_of: :addressable
accepts_nested_attributes_for :addresses
belongs_to :addressable, :polymorphic => true
我的
CreditMemo
型号:

has_many :addresses, :as => :addressable, inverse_of: :addressable
accepts_nested_attributes_for :addresses
has_many :addresses, :as => :addressable, inverse_of: :addressable
accepts_nested_attributes_for :addresses
belongs_to :addressable, :polymorphic => true
我的
地址
型号:

has_many :addresses, :as => :addressable, inverse_of: :addressable
accepts_nested_attributes_for :addresses
has_many :addresses, :as => :addressable, inverse_of: :addressable
accepts_nested_attributes_for :addresses
belongs_to :addressable, :polymorphic => true
我很确定问题出在我的模式底部的这一行:

add_foreign_key "addresses", "customers", column: "addressable_id"

我不知道如何解决这个问题,也不知道它应该是什么。

您不能将外键与多态关联一起使用,因此您必须从模式中删除外键,您可以使用
删除外键

只需使用以下内容编写新的迁移:

remove_foreign_key :addresses, :customers