Before_destroy not called

I have a situation where it seems rails should delete dependent
records but does not:

class Order < ActiveRecord::Base
has_many :line_items, :dependent => :destroy
end

class LineItem < ActiveRecord::Base
belongs_to :order
end

o=Order.new
li=LineItem.new
o.line_items<<li
o.save!
Order.delete(o.id)

This deletes the order but not the line item. I did a little debugging
and see that rails adds the code (associations.rb):

      case reflection.options[:dependent]
        when :destroy, true
          module_eval "before_destroy '#{reflection.name}.each { |

o| o.destroy }’"

but if I add a before_destroy to Order, it is never called. There was
another post on this but I didn’t really see an explanation for this.
More of a workaround.

Rails is great and all, but running into what seems to be very simple
cases that don’t work and having to step thru very cryptic rails code
isn’t much fun.

I guess I’ll add cascade/delete to the database tables…

delete skips any callbacks, use destroy if you want the callbacks to get
invoked.

o=Order.new
li=LineItem.new
o.line_items<<li
o.save!
o.destroy

dailer wrote:

        when :destroy, true

I guess I’ll add cascade/delete to the database tables…


Sincerely,

William P.

http://www.billpratt.net
[email protected]

ugh. that was too easy.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs