Beginner :conditions question


#1

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
using:

@upcoming_birthdays = People.find(:all, :conditions =>
“birthdate.strftime(’%M’) = Time.today.strftime(’%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!


#2

Hello,

Firstly, numeric months are “%m” , so maybe changing “M” to “m” will be
enough.

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

:conditions => [ “birthdate BETWEEN Time.now.strftime(”%Y-%m-%01
%H:%M:%S") AND Time.now.strftime("%Y-%m-%31 %H:%M:%S")" ])

Actually, rails extends Date and Time in neat ways. You could write
something like Time.now.at_beginning_of_month.to_s(:db) instead of
mucking
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.

J