2

I have a database full of some information, but I need to go into the rails console and add one integer to a specific data set in my local database.

I have a column named times_used, within that column I need to add one more integer into it.

I've tried,

p.update_attribute(:times_used, add: 1)
p.update_attribute(:times_used, add: :1)
p.update_attribute(:times_used, add: [:1])
p.update_attribute(:times_used, + 1)
p.update_attribute(:times_used) + 1

I'm wondering, what I am trying to do, is possible? Do I need to loop through these? Much thanks to anybody for taking a quick look at this.

2 Answers 2

2

You can do something like this:

MyModel.where(id: 1).update_all('times_used = times_used + 1')
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, I like how your approach allows you to make changes to everything, and also individual elements as well.
1

You can use the increment! method, take a look: http://apidock.com/rails/ActiveRecord/Base/increment!

//would be something like
p.increment!(:times_used)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.