1

我有一个方法如下...

def self.get(code)
 where(code: normalize_code(code)).
 where('coupon_count > 0').
 where('expires_at > Time.now OR expires_at IS NULL').
 take
end

我在“take”行上不断收到错误“参数数量错误(0 代表 1)”。我正在使用 rails 4.0.1 是导致问题还是我遗漏了什么?

编辑查看 4.0.1 http://rails.documentation.codyrobbins.com/4.0.10/classes/ActiveRecord/FinderMethods.html#method-i-take的文档

我将方法更新为

def self.get(code)
 where(code: normalize_code(code)).
 where('coupon_count > 0').
 where('expires_at > Time.now OR expires_at IS NULL').
 take(1)
end

现在我得到了错误

SyntaxError: Unexpected identifier (16722)

错误在“take”行

-更新-

我的错误出现在优惠券计数的 where 方法中。它不在 take 方法中。在接受优惠券之前,我必须弄清楚它不会检查 coupon_count 字段的内容。

4

2 回答 2

1

http://apidock.com/rails/ActiveRecord/FinderMethods/take文档似乎说take默认将返回限制为 1。

Person.take # returns an object fetched by SELECT * FROM people LIMIT 1

但是,错误消息向我建议take需要一个参数。查看此问题(Ruby 中的数组:Take vs Limit vs First)中答案下方的评论,它基本上总结了在 Ruby 1.8.7、1.9.3、2.0.0 或 2.1.0 中没有参数就无法调用 Take .

于 2016-04-08T03:54:18.500 回答
0

既然你只服用一个,为什么不尝试先使用呢。

于 2016-04-08T04:19:20.033 回答