Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails add one to increment SQL value

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])
like image 688
dennismonsewicz Avatar asked Dec 28 '10 19:12

dennismonsewicz


1 Answers

Use increment!:

med.increment!(:screener_viewed)

Make sure that screener_viewed is of type integer in your db.

like image 111
bowsersenior Avatar answered Oct 13 '22 11:10

bowsersenior