Yes, default_scope
is going to be simplest method and will allow you to continue using things like Post.find(:all)
without worrying about deleted entries, but you might want to ask yourself why you're keeping deleted records around anyway. Would you lose anything of value by simply deleting them? If so, there might be a better answer than a "soft-delete". The trouble with soft deletes, for example, argues very heavily against the practice, and provides a few suggestions for avoiding them.
I personally have nothing against default_scope
, but it can get messy, and in the few times that I have used it, I've always had to either go back and remove it, or put in ugly with_exclusive_scope
helper methods in my model to explicitly get around it.
So, while not as easy as Post.find(:all)
, I would recommend using a named_scope
instead
class Post < ActiveRecord::Base
named_scope :active, :conditions => {:deleted => 0}
end
and then using Post.active.find(:all)
or Post.active.find(params[:id])
. I think it conveys the intention of the code more clearly, it then lets you redefine what "active" is in the future if your business logic changes, and it doesn't make you jump through with_exclusive_scope
hoops if you really do want to retrieve Post.find(:all)
(in an admin app, for instance).
Edit: As I suspected, it looks like you're already looking for ways to get around it. :)
Edit 2: You might want to look at the acts_as_paranoid gem, which looks to manage a lot of this stuff for you (while still giving you access to deleted records without using with_exclusive_scope
).