# Searching based on the return values of instance methods

**URL:** <https://www.ruby-forum.com/t/searching-based-on-the-return-values-of-instance-methods/183794>\
**Category:** Rails\
**Created:** [February 20, 2010, 7:31am UTC](https://www.ruby-forum.com/t/searching-based-on-the-return-values-of-instance-methods/183794 "2010-02-20T07:31:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Learn\_by\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@Learn\_by\_D](https://www.ruby-forum.com/u/Learn_by_D)\
**Post date:** [February 20, 2010, 7:31am UTC](https://www.ruby-forum.com/t/searching-based-on-the-return-values-of-instance-methods/183794/1 "2010-02-20T07:31:40Z")

</div>

Can I use something like searchlogic to search for objects based on  
the return values of instance methods? E.g., I’d like to search for  
users of a certain “age” but age is not a column, it’s an instance  
method, returning the age value calculated from the birthdate, which  
is a column.

Is there a quick way like searchlogic to do this or do I have to build  
my own search queries? Thanks.

---

<div class="post-metadata">

**Author:** ![Learn\_by\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@Learn\_by\_D](https://www.ruby-forum.com/u/Learn_by_D)\
**Post date:** [February 21, 2010, 12:07am UTC](https://www.ruby-forum.com/t/searching-based-on-the-return-values-of-instance-methods/183794/2 "2010-02-21T00:07:11Z")

</div>

Converting to datetime object is an excellent idea. Thanks Jarin!

---

<div class="post-metadata">

**Author:** ![Learn\_by\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@Learn\_by\_D](https://www.ruby-forum.com/u/Learn_by_D)\
**Post date:** [February 20, 2010, 9:03am UTC](https://www.ruby-forum.com/t/searching-based-on-the-return-values-of-instance-methods/183794/3 "2010-02-20T09:03:32Z")

</div>

For that particular search, you’ll have to build your own search  
queries. The only other option is to load all records into memory and  
filter by the age instance method, but of course you don’t want to do  
that.

Searchlogic will still make the search easier though. You can convert  
the age from an integer into a datetime object and use it in a  
Searchlogic search like so:

age = 29

User.birthdate\_greater\_than(age.years.ago).birthdate\_less\_than((age -  
1).years.ago).all

Hope this helps!

Jarin U.  
Robot Mode LLC

---

<div class="post-metadata">

**Author:** ![Learn\_by\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@Learn\_by\_D](https://www.ruby-forum.com/u/Learn_by_D)\
**Post date:** [March 8, 2010, 9:26pm UTC](https://www.ruby-forum.com/t/searching-based-on-the-return-values-of-instance-methods/183794/4 "2010-03-08T21:26:04Z")

</div>

marikka! awesome. Thanks for sharing.

---

<div class="post-metadata">

**Author:** ![Learn\_by\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@Learn\_by\_D](https://www.ruby-forum.com/u/Learn_by_D)\
**Post date:** [March 8, 2010, 6:56pm UTC](https://www.ruby-forum.com/t/searching-based-on-the-return-values-of-instance-methods/183794/5 "2010-03-08T18:56:14Z")

</div>

Thanks Jarin for the idea!

I used named scopes in my user model like this:

named\_scope :age\_gt, lambda { |age| { :conditions =\> [‘birthdate \< ?’,  
age.to\_i.years.ago] }}  
named\_scope :age\_lt, lambda { |age| { :conditions =\> [‘birthdate \> ?’,  
age.to\_i.years.ago] }}

now I don’t need to use a special query to achieve this.
