How to create “Upcoming birthdays” module in Rails?

后端 未结 9 2230
栀梦
栀梦 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:09

    I'd have a before_save callback that calculates and stores to the day of the year in the database alongside the birthday.

    You then have a simple query to pull back the next 5 birthdays. Make sure to handle the boundary condition where you are at the end of the year (I'd check if you don't get 5 results in RoR and then run a new query for the 1st Jan to get some extra birthdays to make it up to 5).

    You will probably want to cache the results so you don't keep rerunning the query if it is on a common page.

提交回复
热议问题