0

I'm rather new to rails. I have a controller that's attempting to update a model using the following code:

@test = Product.find(1)
@test.increment!(:price)

It does successfully retrieve the Product from the database, but does not update the price attribute. Does anyone know why this might be the case, or how I could get more feedback on what the problem is?

Thanks a bunch!
Tristan

edit: price is an integer with value 0. I get the same problem when I set the price with @test.price=50 and then @test.save .

WoodenKitty
  • 6,521
  • 8
  • 53
  • 73

3 Answers3

1

Ah, I found the problem. I was using the paperclip (image attachment) gem, which had a validation blocking any updates. This baby solved it: Smarter paperclip validations

Community
  • 1
  • 1
WoodenKitty
  • 6,521
  • 8
  • 53
  • 73
0

increment bypasses model validations, so it's possible that incrementing "price" by 1 is invalidating the model, preventing it from being saved. It's hard to tell without more context about the model and its price attribute. You could try the non-bang version of increment, call save on it manually, and then inspect the model object to see if there were validation errors.

Jimmy
  • 35,686
  • 13
  • 80
  • 98
  • Hi. I have also tried settings price another way and then calling save. I've tried checking the object for errors, using code that I've since lost, and couldn't see any. Increment and save both return false for me, but when I try those methods using the rails console, they return true. – WoodenKitty Jan 04 '11 at 03:11
0

What is the db type of the "price attribute?

Increment only works on number based attributes (http://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-increment%21).

diedthreetimes
  • 4,086
  • 26
  • 38