zerosum dirt(nap)

evolution through a series of accidents

zerosum dirt(nap)

Down w/ with_scope

November 08, 2006 by nap · Comments

We need to delete some stuff. But when we delete items, we don’t really delete them because hey, we don’t have to (we might want them later, no?) So we have an attribute on the model, deleted, which is null if the item is current. When we delete an item, we update_attributes to set deleted to the current datetime.

Yes, yes, I know, this is really common stuff. But hold on, this is where it gets kind of interesting. We want to call find on the model, like always, and only get back current items that have not been deleted. Oh, and we don’t want to have to pass in the condition every time, because that’s just gross. We’d just like to, well, add the condition to the scope. This is where the magic of with_scope can help us out.

Let’s declare a method find_current on the model:

def self.find_current
  find(:all, :conditions => "deleted IS NULL")

Well that works for our base case (finding all current items) but it’s pretty inflexible. What if I want to pass in an ID, specify a limit, additional conditions, etc?

def self.find_current(*args)
  with_scope(:find => { :conditions => "deleted IS NULL" }) do

The answer is to use with_scope, which allows us to specify conditions here that will apply throughout the course of the block. Now we can do things like:

Item.find_current(:all, :limit => 10)
Item.find_current(:first, :conditions => "name LIKE '%perishable%'")

Which invoke the find_current method on the model with our additional parameters and includes our deleted condition in the scope automatically.

K, so still not exactly rocket science, but it is a nice, clean solution to a common issue for sure. Unfortunately, with_scope isn’t found in my copy of Agile Web Development with Rails, which is my main reason for noting it here. Hopefully it’ll be in v2! Some other helpful bloggers are out there though, check out Ryan Daigle’s blog, which is where Ian found the article that got us started on this path.

blog comments powered by Disqus