Extending find() in a horrible way

Hi all,

I’m trying to extend ActiveRecord::Base.find() to include a default
scope with every query… I’ve pasted my dirty attempt below. Surely
theres a better way?

Another problem with the below is that all the dynamic finder methods
aren’t effected.

Any ideas?

cheers
-henry

class LinkCategory < ActiveRecord::Base

def self.find(*args)
options = args.last.is_a?(Hash) ? args.pop : {}
sql = “site_id=’#{some_integer}’”
if options[:conditions] && !options[:conditions].empty?
options[:conditions] += " AND #{sql}"
else
options[:conditions] = “#{sql}”
end
args << options
super(*args)
end

end

check out “with_scope”

http://rubyonrails.com/rails/classes/ActiveRecord/Base.html#M000892

however, read this as well. DHH has decided to make with_scope
protected as of 2.0, so it could affect how you use it.

http://www.mail-archive.com/[email protected]/msg00579.html

On 11/12/06, [email protected]

On 11/20/06, Chris H. [email protected] wrote:

check out “with_scope”

http://rubyonrails.com/rails/classes/ActiveRecord/Base.html#M000892

however, read this as well. DHH has decided to make with_scope
protected as of 2.0, so it could affect how you use it.

http://www.mail-archive.com/[email protected]/msg00579.html

def find_in_site(*args)
with_scope :find => { … } do
find *args
end
end


Rick O.
http://weblog.techno-weenie.net
http://mephistoblog.com

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs