searching based on the return values of instance methods

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.

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 Udom Robot Mode LLC

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

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.

marikka! awesome. Thanks for sharing.