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. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Jarin Udom
2010-Feb-20 08:02 UTC
Re: searching based on the return values of instance methods
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 On Feb 19, 10:30 pm, Ease Bus <ease...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Vincent P
2010-Feb-20 23:06 UTC
Re: searching based on the return values of instance methods
Converting to datetime object is an excellent idea. Thanks Jarin! On Feb 20, 12:02 am, Jarin Udom <ja...-vTN78maM9PiELgA04lAiVw@public.gmane.org> wrote:> 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 > > On Feb 19, 10:30 pm, Ease Bus <ease...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > 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.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
marikka
2010-Mar-08 16:35 UTC
Re: searching based on the return values of instance methods
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. On 21 helmi, 01:06, Vincent P <ease...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Converting to datetime object is an excellent idea. Thanks Jarin! > > On Feb 20, 12:02 am, Jarin Udom <ja...-vTN78maM9PiELgA04lAiVw@public.gmane.org> wrote: > > > 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 > > > On Feb 19, 10:30 pm, Ease Bus <ease...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > 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.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Ease Bus
2010-Mar-08 20:24 UTC
Re: Re: searching based on the return values of instance methods
marikka! awesome. Thanks for sharing. On Mon, Mar 8, 2010 at 8:35 AM, marikka <marek.miettinen-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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. > > > > On 21 helmi, 01:06, Vincent P <ease...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> Converting to datetime object is an excellent idea. Thanks Jarin! >> >> On Feb 20, 12:02 am, Jarin Udom <ja...-vTN78maM9PiELgA04lAiVw@public.gmane.org> wrote: >> >> > 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 >> >> > On Feb 19, 10:30 pm, Ease Bus <ease...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >> > > 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. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.