ActiveRecord: Does destroy_all execute callbacks for associated records? -
i have 4 models:
class order < webdatabase has_many :shipments class shipment < webdatabase belongs_to :order has_many :line_items, :order => "id", :dependent => :destroy class lineitem < webdatabase belongs_to :shipment has_many :line_item_messages, :dependent => :destroy class lineitemmessage < webdatabase belongs_to :line_item so if did @order.shipments.destroy_all, there chain of destroys @order.shipments.line_items destroyed , @order.shipments.line_items.line_item_messages destroyed because of :dependent => :destroy on each of models?
yes.
from docs (emphasis own)
destroy_all(conditions = nil)
public
destroys records matching conditions instantiating each record , calling destroy method. each object’s callbacks executed (including :dependent association options , before_destroy/after_destroy observer methods). returns collection of objects destroyed; each frozen, reflect no changes should made (since can’t persisted).
note: instantiation, callback execution, , deletion of each record can time consuming when you’re removing many records @ once. generates @ least 1 sql delete query per record (or possibly more, enforce callbacks). if want delete many rows quickly, without concern associations or callbacks, use delete_all instead.
Comments
Post a Comment