Ruby on Rails Wednesday, February 19, 2014

You should be using object.destroy and dependent: destroy in the model, instead of delete. Destroy is RESTful and will look to remove your dependent objects in the db, among other things.

-Wale

On Tuesday, February 18, 2014 2:38:35 AM UTC-5, marcin.r...@gmail.com wrote:
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@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/0069abf0-7747-4657-89b4-e9b15b5bbeef%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment