AnApprentice
AnApprentice

Reputation: 110960

Rails - Updating a record?

using heroku, I'm trying to update a record.

This is what I did:

@user = User.find(11)
@user.email = '[email protected]'
@user.save
>> true

But then when I do User.find(11), the email isn't updated? Any ideas why?

Upvotes: 1

Views: 5158

Answers (3)

tbaums
tbaums

Reputation: 592

I would recommend trying @user.reload after you hit @user.save. The suggestion above to try @user.save! (read - "@user dot save bang") is also a good one because it will throw an error if a validation fails when saving @user.

Hope that helps.

Upvotes: 3

Dty
Dty

Reputation: 12273

If you're getting true then you don't really have to worry about the record not being validated. True tells you it did get saved. But it is a good practice to use @user.save! like @rubyprince mentioned. That way, invalid update attempts will be raised.

Also, are you sure it's not getting updated? If you're using a database backend I'd go directly to the database rather than relying on an User.find()

You can also try doing User.find(11).reload to see if that works. You can find some info here about reload. So you can try something like this...

user = User.find(11).reload
puts user.email

And see if the email changed.

Upvotes: 0

rubyprince
rubyprince

Reputation: 17793

Also try @user.save! which will give a error if any validations go wrong. But if @user.save give true, then I dont see any reason why it is not saved into the db.

Upvotes: 0

Related Questions