Forum: Ruby on Rails Beginner :conditions question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community 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
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!
John Robitaille (Guest)
on 2007-03-04 08:36
(Received via mailing list)
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
This topic is locked and can not be replied to.