Reputation: 10631
I have a rake task wich should update a record in the database but to no avail. Does it work diffirently when calling from rake?
here is what I have, it runs without an error, but it does not update the record in the db
task :remove_vintage_from_slugs => :environment do
wine = Wine.find_by_id(22)
wine.slug = "new value"
wine.save
end
wine.save! returns true?
1.9.2-p318 :001 > wine = Wine.find_by_id(23)
1.9.2-p318 :002 > wine.slug = "test"
1.9.2-p318 :003 > wine.save!
=> true
Upvotes: 0
Views: 1992
Reputation: 8807
Two things:
By default, save always run validations. If any of them fail the action is cancelled and save returns false. However, if you supply :validate => false, validations are bypassed altogether.
There’s a series of callbacks associated with save. If any of the before_* callbacks return false the action is cancelled and save returns false.
Check to see whether the save is returning false for some reason in the console. Better still you should use save! (with a bang). With save! validations always run. If any of them fail ActiveRecord::RecordInvalid gets raised.
Upvotes: 1