views:

491

answers:

1

Hi I am having what seems like a very simple problem. I just can't seem to pin down what I need to do.

I have a birth_date that is in the Date format. I want to compare it to Date.today as shown below. The problem is it is coming back false because it wants to compare the year as well. It is a birthday so I don't care about the year just trying to see if birth_date (month and day) is equal to Date.today.day.month.

Any ideas?

bdays = Soldier.find(:all, :conditions => ["birth_date LIKE ?", Date.today] )
+3  A: 

You will need to break up your date, because you want to ignore the year. You will need to use some of the functions given by your SQL provider (the example below uses MySQL):

bdays = Soldier.find(:all, :conditions => ["DAY(birth_date) = ? AND MONTH(birth_date) = ?", Date.today.day, Date.today.month])

if you're using SQLite (rails' default database), it will be a bit more complicated because they don't have a real date type:

bdays = Soldier.find(:all, :conditions => ["STRFTIME('%d', birth_date) = ? AND STRFTIME('%m', birth_date) = ?", Date.today.day, Date.today.month])
jamuraa
Hi, This was a great help, thanks so much. One problem I am still having is for SQLite the STRFTIME('%m', birth_date) = ? will return the month of January as 01 and then Date.today.month as 1 causing a false. I have searched all over trying to find someone else with this issue.
looloobs