Topic: Extending find() with default scopes.. (i have an example)

Hi there,

I'm trying to extend find() so all calls have some default scope.

I've included my current version below.. but its really ugly. Someone must have tried this.. any better solutions out there?

(At the moment the extra scope hardly warrants the approach.. I do plan to use it for more complicated stuff than LIMIT)

cheers
-h

class MyModel < ActiveRecord::Base
 
  def self.find(*args)
    options = args.last.is_a?(Hash) ? args.pop : {}
    sql = " LIMIT 10"
    if options[:conditions] && !options[:conditions].empty?
      options[:conditions] += " AND #{sql}"
    else
      options[:conditions] = "#{sql}"
    end
    args << options
    super(*args)
  end
 
end

Re: Extending find() with default scopes.. (i have an example)

Danger wrote an excellent tutorial on how to make a plugin that does this.

Railscasts - Free Ruby on Rails Screencasts