How to create “Upcoming birthdays” module in Rails?

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

    If you're on Oracle, you can do it without creating a new column. IMO it's a smell to create a column that contains data you already have.

    The SQL's a bit ugly - I'm sure there's a more elegant way to do it. Generally in these cases I'd ask my DBA friends for advice.

    User.find(:all,
      :conditions => 
        "TO_NUMBER(TO_CHAR(dob, 'MMDD')) >= TO_NUMBER(TO_CHAR(SYSDATE, 'MMDD'))",
      :order => "TO_NUMBER(TO_CHAR(dob, 'MMDD'))",
      :limit => 5)
    

    Some people think a duplicate column is faster, but if you have enough user data that speed's an issue, you should benchmark the duplicate column against a table without it that has a functional index on TO_NUMBER(TO_CHAR(dob, 'MMDD')).

提交回复
热议问题