【问题标题】:Wrong number of Arguments (0 for 1) Error using Rails Scope to find all in current month参数数量错误(0 表示 1)使用 Rails Scope 查找当前月份的所有参数时出错
【发布时间】:2015-04-18 12:18:51
【问题描述】:

我是范围的新手,在创建它时发现我认为可以在这里工作 Rails scope created within month。但是,我下面的代码给出了错误数量的参数(0 代表 1),我不确定为什么。感谢您就此事提供的任何帮助。
给出问题的范围如下。

scope :contract_out,  -> (date) { where("DATE_PART('month', contract_out_date) = ?", date.month) }

【问题讨论】:

    标签: ruby-on-rails-4 named-scope


    【解决方案1】:

    要获取当前月份内的所有记录,请使用以下范围:

    scope :contract_out, where("contract_out_date >= ? AND contract_out_date < ?", Date.today.beginning_of_month, Date.today.end_of_month)
    

    要在任何月份工作,不仅是当前月份,范围将被修改为接受 1 个属性:

    scope :contract_out, ->(date) {where("contract_out_date >= ? AND contract_out_date < ?", date.beginning_of_month, date.end_of_month)}
    

    【讨论】:

    • 这要感谢一群 mohamed-stark!只需要使其可调用并且可以使用!
    • 谢谢,我将答案扩展到任何日期,请在第二个范围内检查。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-27
    • 1970-01-01
    相关资源
    最近更新 更多