Ruby on Rails
Wednesday, February 19, 2014
You're calling delete_all on an association, even if it's weird, for rails, deleting an association means only breaking the links between the models, not effectively destroying the records. Semantically, this makes sense, but I grant you that it's a bit counter intuitive.
On Tuesday, February 18, 2014 8:38:35 AM UTC+1, marcin.r...@gmail.com wrote:
-- On Tuesday, February 18, 2014 8:38:35 AM UTC+1, 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/9eff3c12-93de-4189-98dd-9a84b847f7a6%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment