Reputation: 25552
I am trying to add one to the already incremented field in the DB, but I am getting the following error: NoMethodError (undefined method
+' for false:FalseClass):`
Code:
med = Media.find(params[:media_id])
med.update_attributes({:screener_viewed => med.screener_viewed + 1})
I am just wanting to add 1
to the current value of screener_viewed, but can't get it to work.
Working Code:
Media.increment_counter(:screener_viewed, params[:media_id])
Upvotes: 6
Views: 5072
Reputation: 769
Careful about using increment! I does not support concurrent request.
http://apidock.com/rails/ActiveRecord/Base/increment!
Upvotes: 0
Reputation: 8840
Could you provide the migration definition for the Media model? It looks like you've defined it as a Boolean, which does not support the + operator:
irb(main):002:0> false + 1
NoMethodError: undefined method `+' for false:FalseClass
from (irb):2
Upvotes: 0
Reputation: 12574
Use increment!
:
med.increment!(:screener_viewed)
Make sure that screener_viewed
is of type integer in your db.
Upvotes: 16
Reputation: 66901
Examine the error message carefully. It says there is no addition method defined for the FalseClass, which means that screener_viewed contains a boolean value of false. Did you expect it to contain an integer, or are you trying to flip the value to true?
Upvotes: 0