在范围内使用 find_by_sql 会出现错误:“2017-01-01”的未定义方法`value_for_database':字符串

Cj *_*aro 2 sql activerecord ruby-on-rails

当我尝试从某个日期范围进行搜索时出现此错误。

这是我的模型:

def self.search(search)
  if search
    @policies = Policy.find_by_sql("acct_ent_date IN ?", start_date..end_date)
  else
    limit(10)
  end
end
Run Code Online (Sandbox Code Playgroud)

Pau*_*rth 7

只是为了帮助到这里的其他人:如果您想将查询参数与 一起使用find_by_sql,您需要将所有参数放入一个数组中,如下所示:

Policy.find_by_sql(["acct_ent_date IN ?", start_date..end_date])
Run Code Online (Sandbox Code Playgroud)

我从未真正见过将范围作为查询参数传递,因此我无法对此发表评论,但一般而言,“未定义的方法 `value_for_database'”错误来自未将参数包装在数组中。