Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.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 轨道6:Can';不要删除嵌套模型。随机插入语句_Ruby On Rails_Postgresql_Destroy_Ruby On Rails 6_Accepts Nested Attributes - Fatal编程技术网

Ruby on rails 轨道6:Can';不要删除嵌套模型。随机插入语句

Ruby on rails 轨道6:Can';不要删除嵌套模型。随机插入语句,ruby-on-rails,postgresql,destroy,ruby-on-rails-6,accepts-nested-attributes,Ruby On Rails,Postgresql,Destroy,Ruby On Rails 6,Accepts Nested Attributes,我在Postgres中使用Rails 6,但在删除嵌套模型时遇到问题。 删除关联后将生成随机插入语句 让我解释一下我的设置 迁移 class CreateEntries < ActiveRecord::Migration[6.0] def change create_table :entries do |t| t.string :name t.timestamps end end end class Cards < ActiveRec

我在Postgres中使用Rails 6,但在删除嵌套模型时遇到问题。 删除关联后将生成随机插入语句

让我解释一下我的设置

迁移

class CreateEntries < ActiveRecord::Migration[6.0]
  def change
    create_table :entries do |t|
      t.string :name
      t.timestamps
    end
  end
end

class Cards < ActiveRecord::Migration[6.0]
  def change
    create_table :cards do |t|
      t.string :card_number
      t.belongs_to :entry, null: true, foreign_key: true
      t.timestamps
    end
  end
end
这些是日志

(0.2ms)  BEGIN

ConcessionCard Load (0.2ms)  SELECT "cards".* FROM "cards" WHERE "cards"."entry_id" = $1 LIMIT $2  [["entry_id", 1], ["LIMIT", 1]]

Card Destroy (0.4ms)  DELETE FROM "cards" WHERE "cards"."id" = $1  [["id", 2]]

Card Create (0.6ms)  INSERT INTO "cards" ("entry_id", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id"  [["entry_id", 1], ["created_at", "2019-09-06 13:50:41.100718"], ["updated_at", "2019-09-06 13:50:41.100718"]]

(0.3ms)  COMMIT
为什么在delete调用之后生成insert?这甚至不是一次倒退


注意:我在属于迁移的卡片中尝试了null:true和null:false。我还尝试在卡片模型中的“属于”to:entry语句中设置optional:true,除非在
Card\u属性中包含
id
,否则Rails会将其视为一条新记录,因此它只是用新创建的
Card
替换
has\u one
(由于您的
dependent::destroy
选项删除了现有的关联


最好在表单部分/视图中使用
form.fields\u for:card
块,它将自动为现有卡添加隐藏的
id
标记。

请帮助,我快疯了。您在条目模型上有设置相关卡的代码吗?您能尝试用
@Entry.attribute替换
更新
调用吗es=…;@entry.save
?在调用
save
之前,您可以使用byebug或pry rails检查对象。感谢您的响应,我非常感谢您的帮助。如果我将卡id包括在卡属性中,则不会执行删除操作。以下是参数:“entry”=>{“card\u attributes”=>{“id”=>“2”,“销毁”=>“true”}。但是,如果卡id被排除在卡属性之外,则会按照您的建议执行删除操作,然后执行插入操作。谢谢您,斯马蒂先生。这就解决了问题。我想给您买杯啤酒。请告诉我如何操作。
class EntriesController < ApplicationController
    before_action :set_entry

    def update
       @entry.update(entry_params)
    end

    def set_entry
       @entry = Entry.find(params[:id])
    end

    def entry_params
      params.require(:entry).permit(:name,
        card_attributes: [:id, :card_number, :_destroy]
      )
    end
end
Parameters: {"authenticity_token"=>"CQ...Ucw==", "entry"=>{"card_attributes"=>{"_destroy"=>"true"}}, "id"=>"1"}
(0.2ms)  BEGIN

ConcessionCard Load (0.2ms)  SELECT "cards".* FROM "cards" WHERE "cards"."entry_id" = $1 LIMIT $2  [["entry_id", 1], ["LIMIT", 1]]

Card Destroy (0.4ms)  DELETE FROM "cards" WHERE "cards"."id" = $1  [["id", 2]]

Card Create (0.6ms)  INSERT INTO "cards" ("entry_id", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id"  [["entry_id", 1], ["created_at", "2019-09-06 13:50:41.100718"], ["updated_at", "2019-09-06 13:50:41.100718"]]

(0.3ms)  COMMIT