接受查询时出错 Rails

getting an error with take in a query Rails

我有一个方法如下...

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" 行收到错误 "wrong number of arguments (0 for 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"行

-更新-

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

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

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

但是错误消息向我暗示 take 需要一个参数。查看此问题 (Arrays in Ruby: Take vs Limit vs First) 中答案下方的评论,该评论基本上总结了在 Ruby 1.8.7、1.9.3、2.0.0 或 2.1.0 中不能在没有参数的情况下调用 Take .

既然你只拿了一个,为什么不试试先用呢。