Rails 3, Refactored Code, and Docs

Posted by Brian in Rails (May 10th, 2011)

If you’ve been doing Rails work for a long time, you’re familiar with the difference between delete and destroy in your ActiveRecord models. If you weren’t, you might look at your code and see what class you
inherited from.

  class User < ActiveRecord::Base
   ...
  end

In Rails 2 and below, the delete and destroy methods were both listed in the documentation for ActiveRecord::Base.

In Rails 3, you'll find these under ActiveRecord::Persistence. And the only way you'd know to look there is by looking through the eight delete methods located in the sidebar at the API.

There's not really a good fix, because the refactoring they did makes a lot of sense. But it also makes these automated documentation tools a lot less useful, especially since there's no indication in ActiveRecord model declarations that a Persistence module is loaded. You'd need to dig into the Rails source, or know who to ask for clarification.

And if you're wondering what the difference is, destroy will invoke callbacks and observers, whereas delete just uses raw SQL to remove the record. Both have their place and it's important to know the difference.

Leave a reply

:mrgreen: :neutral: :twisted: :shock: :smile: :???: :cool: :evil: :grin: :oops: :razz: :roll: :wink: :cry: :eek: :lol: :mad: :sad: