Enhanced find_by method

I saw a recent post by someone suggesting that since
find_by_name_and_school_id_and_deleted_at(‘abc’,34,nil)

was somewhat long-worded, they would prefer a syntax somewhat like

find_by :name => ‘abc’, :school_id => 34, :deleted_at => nil

I thought this might be nice, too. It seems more ruby-like.

Is there anything like this out there? Would there be interest in
creating something like this or gem-ifying it?

It seems like it would allow for even nicer syntaxes, like

find_by :name__includes ‘abc’, :school_id => 34, :deleted_at => nil

Here is the beginnings of one that I coded up.
Thoughts?
Thanks!
-R

class ActiveRecord::Base
def self.find_by specified_conditions, options = {}

note that options could already contain ‘conditions’ so we

accomodate for that
conditions_and_vals = options[:conditions] # an array with [string,
values to insert]
if conditions_and_vals
conditions = conditions[0]
values = conditions[1…-1]
else
conditions = "true "
values = []
end

for column_name, setting in specified_conditions do
column_name = column_name.to_s
if column_name.include? ‘
column_name, desired_search_type = column_name.split(’
’)

   case desired_search_type
   when 'equals'
     style = '='
   when 'contains', 'includes'
     style = 'LIKE'
     setting = "%#{setting}%"
   else
     raise 'undone desired search type ' + desired_search_type
   end
 else # assume it is an equals
   style = '='
 end
 value = column_name

 if setting == nil # like they passed :deleted_at => nil
   raise 'used nil without equals isnt coded yet' unless style ==

‘equals’
style = ‘is’
end

 conditions << " and #{value} #{style} ? "
 values << setting

end

options[:conditions] = [conditions] + values

todo could add :first in here somewhere. For now just do :all

return self.find(:all, options)
end
end

Roger P. wrote:

I saw a recent post by someone suggesting that since
find_by_name_and_school_id_and_deleted_at(‘abc’,34,nil)

was somewhat long-worded, they would prefer a syntax somewhat like

find_by :name => ‘abc’, :school_id => 34, :deleted_at => nil

I thought this might be nice, too. It seems more ruby-like.

Is there anything like this out there? Would there be interest in
creating something like this or gem-ifying it?

You might want to check out squirrel:

http://www.thoughtbot.com/projects/squirrel

I’m not sure how active the project is, but it’s very useful stuff.

You might want to check out squirrel:

http://www.thoughtbot.com/projects/squirrel

I’m not sure how active the project is, but it’s very useful stuff.

Wow thanks.
http://wiki.rubyonrails.org/rails/pages/Searching+and+Query+Plugins
also contained a few useful related plugins (including Squirrel). Looks
like slice_and_dice is closest to what I am looking for.
Thanks!
-R