Forum: Ruby on Rails Beginner :conditions question

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Taylor S. (Guest)
on 2007-03-04 03:40
So I'm trying to grab birthdays for the current month.  But I have to do
some magic inside a condition and this messes up AR.  Here is what I am

@upcoming_birthdays = People.find(:all, :conditions =>
"birthdate.strftime('%M') ='%M')") %>

Of course find is passing this directly and DQL doesn't know what to do
with it.  What is the correct way to pass this condition?  Thanks!
John Robitaille (Guest)
on 2007-03-04 08:36
(Received via mailing list)

Firstly, numeric months are "%m" , so maybe changing "M" to "m" will be

But, :conditions have to be written pretty much in SQL, and I think you
to pass in whole SQL date strings as well. Try something like:

:conditions =>  [ "birthdate BETWEEN"%Y-%m-%01
%H:%M:%S") AND"%Y-%m-%31 %H:%M:%S")" ])

Actually, rails extends Date and Time in neat ways. You could write
something like  instead of
about with strftime at all.

You can read up on all that in
ActiveSupport::CoreExtensions::Time::Calculations in the api docs.

Lemme know if that works for you.

This topic is locked and can not be replied to.