marcin.raczkowski-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2014-Feb-18 07:38 UTC
Why does calling .delete_all on has_many relationship nullifies foreign keys.
I have several years of Rails expirience and this little bit ALWAYS gets me: class Foo < ActiveRecord::Base has_many :bars end class Bar < ActiveRecord::Base belongs_to :foo end # after creating some foos and bars foo.bars.delete_all #=> UPDATE bars SET foo_id = NULL WHERE foo_id = $1 Yes I know I can set :dependant => :delete_all but what's resoning behind this default behaviour? Is it just a legacy stuff? I'd expect foo.bars.delete_all to act the same as Bar.delete_all or Bar.where(foo_id: foo.id).delete_all regardles of the :dependant option. anyone knows why the default behaviour here is to nullify keys? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/9487b425-9ea6-4fd3-b89b-373be55b826c%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.