I am wondering whether this is the designed behavior of ActiveRecord,
to me, it is not the expected one.
I have a simple model design with an Order having many Order Items and
Order Item belonging to an Order. The has_many association on Order has
:dependent => :destroy option.
Also, the Order Item has a before_destroy function that would check the
price of the Order Item. If the price is above 100, it returns false and
does not allow the Order Item to be destroyed.
The classes codes has as follows:
class Order < ActiveRecord::Base
has_many :order_items, :inverse_of => :order, :dependent => :destroy
class OrderItem < ActiveRecord::Base
attr_accessible :price, :order_id
belongs_to :order, :inverse_of => :order_items
if price <= 100
puts “I have low price…I can be deleted”
puts “I have high price…I cannot be deleted”
The problem is that when I am issuing a destroy on an instance of an
that has an order item that has price > 100, then the order item is not
deleted, but the order does. Here are the console commands and output:
Loading development environment (Rails 3.2.8)
1.9.3-p194 :001 > o = Order.new :order_code => ‘first order’
=> #<Order id: nil, order_code: “first order”, created_at: nil,
1.9.3-p194 :002 > o.save!
SQL (0.2ms) INSERT INTO
updated_at) VALUES (‘2012-08-20 08:14:20’, ‘first order’, ‘2012-08-20
1.9.3-p194 :003 > o.order_items << OrderItem.new(:price => 110)
SQL (0.3ms) INSERT INTO
updated_at) VALUES (‘2012-08-20 08:14:31’, 5, 110,
OrderItem Load (0.3ms) SELECT
=> [#<OrderItem id: 4, order_id: 5, price: 110, created_at: “2012-08-20
08:14:31”, updated_at: “2012-08-20 08:14:31”>]
1.9.3-p194 :004 > o.destroy
I have high price…I cannot be deleted
SQL (1.0ms) DELETE FROM
=> #<Order id: 5, order_code: “first order”, created_at: “2012-08-20
08:14:20”, updated_at: “2012-08-20 08:14:20”>
1.9.3-p194 :005 >
Can somebody explain to me, why the failure of the dependent object
method does not trigger a rollback?
Thanks in advance