Aniee
Aniee

Reputation: 81

fetch record from database according to date_of_birth in Rails 4

I want to create a query for fetching records from database. In Manager Model, Manager Table has date_of_birth column. Query is:

   Manager.all.where(:date_of_birth => Date.now)

Through Query whole format of date_of_birth column compare to current date in Manager Controller's Index action.But my requirement is to compare month and day of date_of_birth column to today's day and month.How can I do that? Please help me. Thanks in advance.

Upvotes: 1

Views: 127

Answers (1)

Caillou
Caillou

Reputation: 1500

You can use the SQL methods MONTH() and DAY() for your query like this :

Manager.all
  .where("MONTH(date_of_birth) = (?)", Date.now.month)
  .where("DAY(date_of_birth) = (?)", Date.now.day)

Documentation :

Upvotes: 1

Related Questions