【问题标题】:How to filter rails model by month如何按月过滤rails模型
【发布时间】:2018-05-10 06:47:26
【问题描述】:

我只想获取属于特定月份的模型记录。我正在使用:

Order.where(created_at.strftime("%B"): "April")

其中created_atDateTimecreated_at.strftime("%B") 给了我一个月,但它不起作用。有什么选择吗?

【问题讨论】:

    标签: ruby-on-rails sqlite


    【解决方案1】:

    您可能必须在计划 SQL(而不是 activerecord DSL)中执行此操作:

    Order.where("strftime('%m', created_at) = ?", 'April')
    

    This uses the SQLite function to extract the month name

    (我有一段时间没做 Rails,如果这不起作用,请告诉我)

    【讨论】:

    • 你的意思是 - Order.where("strftime('%B', created_at) = ?", 'April')。它不工作
    • Order.where('extract(month from created_at)=?', 4) 有效,但我想按月份名称而不是月份编号搜索:-
    • 它看起来像SQLite doesn't getting the month name,所以您可能必须自己将其映射到一个数字。
    猜你喜欢
    • 1970-01-01
    • 2018-08-18
    • 1970-01-01
    • 1970-01-01
    • 2019-04-14
    • 2015-01-04
    相关资源
    最近更新 更多