How to create “Upcoming birthdays” module in Rails?

后端 未结 9 2234
栀梦
栀梦 2020-12-29 17:46

I have a table \"users\" with a column \"date_of_birth\" (DATE format with day, month, year). In frontend I need to list 5 upcoming birthdays.

Spent ages trying to w

9条回答
  •  情歌与酒
    2020-12-29 18:18

    It's better to use SQL to make this query:

    next_month = (Date.today + 1.month).month # or use any other integer
    
    users_having_birhtday_next_month = 
      User.where("EXTRACT(MONTH FROM date_of_birth) = ?", next_month)
    

    Note: EXTRACT - PostgreSQL function

提交回复
热议问题