Wednesday, September 30, 2009

default_scope and is_paranoid

default_scope at first seems like a great idea (was Paul D's idea to use locally). At first it sounds kind of hackish and scary, but it seems simple and DRY to look at only the data that matters without the overhead of sometimes not-as-DRY named scopes. If you rarely need to access data outside of scope, you can temporarily "override" it.

However, then you run into someone else that uses it (like is_paranoid) and quickly realize that it can be evil. The deal is that Rails for whatever reason doesn't just merge the default_scopes that are being defined as you'd hope and (possibly) expect. So for now we have to settle with having hacked versions of things doing the merging of various scopes, at least until Rails gets the hint that accidentally redefining it is a bad thing.

If you're looking for a version of is_paranoid that automatically merges its default_scope with yours/others, then check out:
* http://github.com/grioja/is_paranoid

(Thanks to Jim Wood that pointed me at this that had a comment at the bottom with the fixed forked version of is_paranoid)

grioja's fork of is_paranoid just works as you'd expect. You can just do the following and the default_scope of is_paranoid and your default_scope work together:

class GroupProject < ActiveRecord::Base
  # is_paranoid MUST!!! come before anything else
  is_paranoid
  default_scope :conditions => {:some_attribute_to_scope_by => 'some value'}
  ...
end

No comments: