Rails关联has_one,through,dependent destroy不销毁相关对象



我在Rails网站上找不到涵盖这个特定用例的文档。假设正常的has_one会工作(因为它这么说)。我还没试过。

给定关联的两个模型和一个联接表,我希望dependent: :destroy会在销毁父模型时删除联接表行。

链式删除仅适用于has_many关系(此处未显示此代码,但可以很容易地修改模型以实现它)。

我相信会有一个基于使用具有唯一性约束的has_many(可能在联接表上)的解决方法。但我认为我不应该这么做!

IRB复制:

000 > a = ModelA.create(name: "Fruity")
( output )
001 > b = ModelB.create(interesting_thing: "Tennis")
( output )
002 > a.model_b = b
( output )
003 > pp ModelAModelB.all
ModelAModelB Load (0.3ms)  SELECT "model_a_model_bs".* FROM "model_a_model_bs"
[#<ModelAModelB:0x005609f4fa5128
id: 1,
model_a_id: 1,
model_b_id: 1,
created_at: Fri, 27 Jan 2017 13:37:15 UTC +00:00,
updated_at: Fri, 27 Jan 2017 13:37:15 UTC +00:00>]
004 > a.destroy
(0.1ms)  begin transaction
SQL (3.0ms)  DELETE FROM "model_as" WHERE "model_as"."id" = ?  [["id", 1]]
(4.5ms)  commit transaction

型号:

class ModelA < ApplicationRecord
has_one :model_a_model_b
has_one :model_b, through: :model_a_model_b, dependent: :destroy
end
class ModelB < ApplicationRecord
has_one :model_a_model_b
has_one :model_a, through: :model_a_model_b, dependent: :destroy
end

class ModelAModelB < ApplicationRecord
belongs_to :model_a
belongs_to :model_b
end

架构:

ActiveRecord::Schema.define(version: 20170127100855) do
create_table "model_a_model_bs", force: :cascade do |t|
t.integer  "model_a_id"
t.integer  "model_b_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["model_a_id"], name: "index_model_a_model_bs_on_model_a_id"
t.index ["model_b_id"], name: "index_model_a_model_bs_on_model_b_id"
end
create_table "model_as", force: :cascade do |t|
t.string   "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "model_bs", force: :cascade do |t|
t.string   "interesting_thing"
t.datetime "created_at",        null: false
t.datetime "updated_at",        null: false
end
end

Gemfile:

source 'https://rubygems.org'
git_source(:github) do |repo_name|
repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
"https://github.com/#{repo_name}.git"
end

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.0.1'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use Puma as the app server
gem 'puma', '~> 3.0'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platform: :mri
end
group :development do
# Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
gem 'web-console', '>= 3.3.0'
gem 'listen', '~> 3.0.5'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

您还需要对联接模型进行依赖销毁。

class ModelA < ApplicationRecord
has_one :model_a_model_b, dependent: :destroy
has_one :model_b, through: :model_a_model_b, dependent: :destroy
end

或者更好的是,使用on_delete: :cascade的外键来让数据库删除它们。

相关内容

  • 没有找到相关文章

最新更新