0
使用Rails 3.2。我聽說delete
比destroy
快,而delete
不會回調,但不知道如何使用delete
。我有以下的Rake任務:在Rails中銷燬一組對象及其同夥
# shop.rb
class Shop < ActiveRecord::Base
has_many :reviews, dependent: :destroy
end
# review.rb
class Review < ActiveRecord::Base
belongs_to :shop
end
# shop.rake
desc "Delete shops"
task :destroy_shops => :environment do
Shop.find_by_sql("SELECT id FROM shops WHERE shop_status = 0").destroy_all
end
當然,這是行不通的,因爲destroy_all
沒有對象的數組上運行。快速銷燬shops
及其員工的最佳方法是什麼?請注意,review
只是shop
的衆多同事之一。
另請注意,我的SQL
已被簡化。我有更復雜的查詢連接表來檢索不需要的shops
。我只是簡化了這個問題。
http://nick.recoil.org/2006/08/searching-for-一個護欄-delete_by_sql-方法/ –