按生日字段获取 18 岁以上用户的范围 (Rails 3.2)

Scope to get users older then 18 years old by birthday field (Rails 3.2)

我需要编写范围来帮助我获取年龄大于 18 岁的用户,但我在数据库中没有年龄字段。 我的解决方案不起作用 =(

 1. scope :adult, -> { where('Time.now.utc.to_date.year - birthday.to_date.year >= 18') }

 2. scope :adults, -> { where('birthday >= ?', age) }

    def age
      Date.today - 18.years
    end

试试这个

scope :adults, -> { where('birthday <= ?', 18.years.from_now) }