Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails update_all fields and increment count by 1

I have a column in my mysql table called download_count of type int.

Now I want to use update_all to update some fields with some values, but also increment the download_count along with it.

I have tried the following syntax:

find_record.update_all(username: username, download_date: download_date, "download_count= download_count + 1")

But I beleive the above acts like a where clause. So the download_count does not get updated. I want the username, download_date and the download_count to be updated by the above query.

Can someone point me to the correct syntax?

like image 698
newbie Avatar asked Feb 12 '23 14:02

newbie


1 Answers

Try

find_record.update_all(["username=?, download_date=?, download_count=download_count+1", username, download_date])

Second argument to update_all is a condition.

like image 187
usha Avatar answered Feb 15 '23 09:02

usha