Я использую Rails 6 с Postgres и не могу удалить вложенную модель. После удаления связи создается случайный оператор вставки.
Позвольте мне объяснить мою установку.
Миграции
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
Модели
class Entry < ApplicationRecord
has_one :card, dependent: :destroy
accepts_nested_attributes_for :card, allow_destroy: true
end
class Card < ApplicationRecord
belongs_to :entry
end
Контроллер
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
Почему после вызова удаления создается вставка? Это даже не откат.
Примечание. Я пробовал как null: true, так и null: false при миграции Cards own_to. Я также пробовал установить optional: true в операторе own_to: entry в модели Card
update
на@entry.attributes = ...; @entry.save
? вы можете использовать byebug или pry rails для проверки объекта перед вызовомsave
. - person arieljuod   schedule 06.09.2019