1

I have a method as follows...

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

I keep getting the error "wrong number of arguments (0 for 1)" on the "take" line. I am using rails 4.0.1 is that causing the problem or am I missing something?

EDIT Looking at the docs for 4.0.1 http://rails.documentation.codyrobbins.com/4.0.10/classes/ActiveRecord/FinderMethods.html#method-i-take

I updated the method to

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

Now I get the error

SyntaxError: Unexpected identifier (16722)

The error is on the "take" line

-UPDATE-

My error is in the where method for coupon_count. It is not in the take method. I have to figure out what it wont check the coupon_count field before accepting the coupon.

SupremeA
  • 1,519
  • 3
  • 26
  • 43

2 Answers2

1

http://apidock.com/rails/ActiveRecord/FinderMethods/take the docs seems to say that take defaults to limit the return to 1.

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

However the error message suggests to me that take requires an argument. Check out the comments to below the answer in this question (Arrays in Ruby: Take vs Limit vs First) which basically summarises that Take cannot be called without an argument in Ruby 1.8.7, 1.9.3, 2.0.0 or 2.1.0.

Community
  • 1
  • 1
Ben Hawker
  • 949
  • 8
  • 15
0

Since you're only taking one, why not try using first instead.

Tommy Mertell
  • 105
  • 1
  • 8
  • tried it and got the same answer or error I should say – SupremeA Apr 08 '16 at 04:25
  • Well, unexpected identifier usually means there's a syntax issue or your giving it a variable that comes from a class the method isn't in. Just out of curiosity, primarily because I've never end my Rails queries with dots, what are those supposed to do? – Tommy Mertell Apr 08 '16 at 04:31
  • they are chaining the methods together for 1 query – SupremeA Apr 08 '16 at 04:32
  • so maybe my syntax is wrong. I want it to say check the field 'coupon_code and if it is greater than or = to 1' then get that 1 – SupremeA Apr 08 '16 at 04:36