【问题标题】:fetch record from database according to date_of_birth in Rails 4根据 Rails 4 中的 date_of_birth 从数据库中获取记录
【发布时间】:2015-10-16 08:49:16
【问题描述】:

我想创建一个从数据库中获取记录的查询。在经理模型中,经理表有 date_of_birth 列。查询是:

   Manager.all.where(:date_of_birth => Date.now)

通过查询 date_of_birth 列的整个格式与 Manager Controller 的索引操作中的当前日期进行比较。但我的要求是将 date_of_birth 列的月份和日期与今天的日期和月份进行比较。我该怎么做?请帮我。提前致谢。

【问题讨论】:

    标签: mysql ruby-on-rails-4 activerecord model


    【解决方案1】:

    您可以使用 SQL 方法 MONTH()DAY() 进行如下查询:

    Manager.all
      .where("MONTH(date_of_birth) = (?)", Date.now.month)
      .where("DAY(date_of_birth) = (?)", Date.now.day)
    

    文档:

    【讨论】:

    • 非常感谢。得到你的答案后,我使用这个查询Manager.all.where("MONTH(date_of_birth) = (?) and DAY(date_of_birth) = (?)", Date.today.month ,Date.today.day)
    • 是的,生成相同的sql请求。
    猜你喜欢
    • 1970-01-01
    • 2011-05-29
    • 1970-01-01
    • 2013-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-24
    • 1970-01-01
    相关资源
    最近更新 更多