Search code examples
ruby-on-railsmongoidtake

getting an error with take in a query Rails


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.


Solution

  • 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.