我想有一个今天的生日和即将到来的生日(汤莫罗)成员名单。

In users.controller.rb
def birthday
   @birthday_users = User.all.order("created_at DESC").where(:birthday => Date.today)
end

def upcoming_birthday
   @upcoming_birthday_user = User.all.order("created_at DESC").where(:birthday => 1.days.from_now )
end

这些代码只在日期、月份和年份与今天相同时才起作用。
例子:
用户1的生日=2018年10月3日(显示在生日列表上)
用户2的生日=2000年10月3日(不在生日列表中显示)

最佳答案

用户模型
博士后

scope :today_birthday, { where('EXTRACT(month FROM birthday) = ? AND EXTRACT(day FROM birthday) = ?', Date.today.month, Date.today.day).order("created_at DESC") }

scope :upcoming_birthday, { where('EXTRACT(month FROM birthday) = ? AND EXTRACT(day FROM birthday) = ?', 1.days.from_now.month, 1.days.from_now.day).order("created_at DESC") }


scope :today_birthday, { where('MONTH(birthday) = ? AND DAY(birthday) = ?', Date.today.month, Date.today.day).order("created_at DESC") }

scope :upcoming_birthday, { where('MONTH(birthday) = ? AND DAY(birthday) = ?', 1.days.from_now.month, 1.days.from_now.day).order("created_at DESC") }

10-08 05:39
查看更多