我在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]
答案 0 :(得分:2)
你也需要对连接模型进行依赖破坏。
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
的外键让数据库删除它们。