Summing a value in a field with rails

2

Hello! In my users table I have a downloads field. I would like that when the user enters the page I add +1 in that user field.

What is the best way to do this? Do I really need to do 2 querys? get the current value and then add another one, or is there a way to simply add a value to that existing value?

    
asked by anonymous 19.08.2014 / 14:23

1 answer

0

If you look at the logs , you'll find that Devise only searches for the user ( SELECT ... FROM usuarios ... ) once in the request, and then returns cache if requested.

So there's no problem in using:

current_usuario.downloads + 1

to update. Try one of the approaches, if it does not work let me know:

current_usuario.downloads = current_usuario.downloads + 1
current_usuario.save

# ou
Usuario.update(current_usuario.id, downloads: (current_usuario.downloads + 1))
    
19.08.2014 / 14:59