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....
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
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:> 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....
>
>
> >
>
--
Sincerely,
William Pratt
http://www.billpratt.net
billp-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
ugh. that was too easy. On Aug 18, 5:35 pm, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote:> 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: > > 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.... > > -- > Sincerely, > > William Pratt > > http://www.billpratt.net > bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---