Francois
Francois

Reputation: 10631

Rake, Active Record - Rails rake task, update record in database

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

Update

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

Answers (1)

Syed Aslam
Syed Aslam

Reputation: 8807

Two things:

  1. 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.

  2. 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

Related Questions