Rails date compared to Date.today

I have a birth_date variable in Date format. I want to compare it with Date.today, as shown below. The problem is that he is coming back because he wants to compare the year. It's a birthday, so I don't care that the year is 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] ) 
+4
source share
2 answers

You will need to break the date because you want to ignore the year. You will need to use some of the functions provided 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 use SQLite (the default database is rails), it will be a little more complicated because they do not 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]) 
+11
source

Got it that postgres added:

Soldier.where("EXTRACT(DAY FROM birth_date) = ? AND EXTRACT(MONTH FROM birth_date) = ?", Date.today.day, Date.today.month)

+4
source

All Articles