# Enhanced find\_by method

**URL:** <https://www.ruby-forum.com/t/enhanced-find-by-method/132663>\
**Category:** Rails\
**Created:** [March 22, 2008, 8:55pm UTC](https://www.ruby-forum.com/t/enhanced-find-by-method/132663 "2008-03-22T20:55:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [March 22, 2008, 8:55pm UTC](https://www.ruby-forum.com/t/enhanced-find-by-method/132663/1 "2008-03-22T20:55:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [March 22, 2008, 9:47pm UTC](https://www.ruby-forum.com/t/enhanced-find-by-method/132663/2 "2008-03-22T21:47:51Z")

</div>

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](http://www.thoughtbot.com/projects/squirrel)

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

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [March 26, 2008, 8:29pm UTC](https://www.ruby-forum.com/t/enhanced-find-by-method/132663/3 "2008-03-26T20:29:48Z")

</div>

> You might want to check out squirrel:
> 
> [http://www.thoughtbot.com/projects/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](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
