更新:可能存在阻止此配置的gem。这些宝石中没有一个看起来像匪徒,但如果有人认为他们发现了一个,请告诉我。谢谢!
在这里使用Rails 3。我有两个模特。
class Contact < ActiveRecord::Base
has_many :invitations, dependent: :destroy
end
class Invitation < ActiveRecord::Base
belongs_to :contact
end
当我运行以下内容时..
contact = Contact.first // has associated invitations
contact.destroy
联系人已被销毁,但邀请却没有。当我查看日志时,我看到以下sql查询打印输出..
Invitation Load (0.2ms) SELECT `invitations`.* FROM `invitations` WHERE `invitations`.`contact_id` = 51
(0.4ms) BEGIN
(0.6ms) UPDATE `contacts` SET `deleted_at` = '2016-02-25 13:05:38', `updated_at` = '2016-02-25 13:05:38' WHERE `contacts`.`id` = 51
(1.2ms) COMMIT
当我查看数据库时,邀请记录仍然存在。
我的宝石文件......
source 'https://rubygems.org'
gem 'rails', '3.2.22.1'
# testing libraries
group :development, :test do
# fixture replacement
gem 'factory_girl_rails', '~> 4.4.1'
gem 'faker', '~> 1.3.0'
gem 'webrick', '~> 1.3.1'
gem 'rspec-rails', '~> 2.14.2'
gem 'shoulda-matchers', '~> 2.6.1'
gem 'spork', '~> 1.0.0rc4'
gem 'shoulda-callback-matchers', '~> 1.1.1'
end
group :assets do
gem 'coffee-rails', '~> 3.2.1'
gem 'compass-rails', '1.1.7'
gem 'sass-rails', '3.2.6'
end
group :development do
gem "haml-rails", ">= 0.3.5"
gem "hpricot", ">= 0.8.6"
gem "ruby_parser", ">= 3.0.1"
gem "quiet_assets", ">= 1.0.1"
#gem "better_errors", ">= 2.1.1"
gem 'guard-rspec', '~> 4.2.9', require: false
gem 'letter_opener', '~> 1.2.0'
end
gem 'bootstrap-sass', '3.2.0.2'
gem 'momentjs-rails', '~> 2.2.1'
# have to throw in this again, the server's sprocket will fail if this gem is missing
gem "bootstrap-sass-rails", ">= 2.3.2.1"
gem 'uglifier', '>= 1.0.3'
gem 'font-awesome-rails', '~> 4.1.0.0'
gem 'google_visualr', '~> 2.5.1'
gem 'angularjs-rails', '~> 1.4.7'
gem 'ngannotate-rails', '~> 0.9.5'
gem 'jquery-rails'
gem 'jquery-ui-rails'
gem "mysql2", "~> 0.3.11"
gem "haml", ">= 3.1.7"
gem "nokogiri", ">= 1.6.3.1"
gem 'slim-rails', '~> 3.0', '>= 3.0.1'
# Handles cron jobs
gem 'whenever', '~> 0.9.4'
gem "annotate", "~> 2.6.2"
gem 'oj', '~> 2.8.1'
gem "devise", "~> 3.0.3"
gem "cancan", ">= 1.6.8"
gem "rolify", ">= 3.2.0"
gem "simple_form", ">= 2.0.4"
gem 'chosen-rails', '~> 1.1.0'
gem 'formtastic', '~> 2.2.1'
gem 'jquery_datepicker', '~> 0.4'
gem 'bettertabs', '~> 1.4.1'
gem 'country_select', '~> 1.3.1'
gem 'capistrano', '~> 2.15.5'
gem 'capistrano-ext', '~> 1.2.1'
gem 'rvm-capistrano', '~> 1.5.1', require: false
gem 'net-ssh', '2.7.0'
gem 'newrelic_rpm', '~> 3.8.0.218'
gem 'rack-cache', '~> 1.2', require: 'rack/cache'
gem 'dragonfly', '~>0.9.15'
gem 'will_paginate', '~> 3.0'
gem 'bootstrap-tooltip-rails', '~> 0.1'
gem 'omnicontacts', '~> 0.3.7'
gem 'roo', '~> 2.1.0'
gem 'roo-xls', '~> 1.0.0'
gem 'rubyzip', '~> 1.1.7'
gem "googlecharts", '~> 1.6.12', require: "gchart"
gem "simple_calendar", "~> 2.0.3"
gem 'rack-google_analytics', '~> 1.0.2'
gem 'rambling-slider-rails', '~> 0.2.0'
gem 'test-unit', '~> 3.0'
#ecomm group from stripe
gem "figaro", ">= 0.6.3"
gem "stripe", '~> 1.17.1'
gem "stripe_event", '~> 1.4.0'
gem 'rest-client', '~> 1.7.2'
此代码有什么问题,如何解决?
答案 0 :(得分:1)
我认为你的应用中的某个人可能已经通过模型中的方法或gem来覆盖destroy的工作方式。检查
之类的内容 def destroy
self.update_attributes(deleted_at: DateTime.current)
end
如果有类似的东西,你必须覆盖它。我首先查看db:schema中带有“deleted_at”列的所有表,然后使用super
向这些模型添加destroy方法来调用ActiveRecord :: Base destroy方法。
答案 1 :(得分:0)
这是由于它用于逻辑删除的偏执宝石。你可以通过以下代码从db中删除
contact = Contact.first // has associated invitations
contact.really_destroy!