Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot save a record in the Console

So to get a record in the User model I run

 @user = User.find(1)

Then to change an attribute:

 @user.active = false

It then returns false

When I run

 @user.save

I get

(0.2ms)  BEGIN
(0.3ms)  SELECT 1 FROM `users` WHERE (LOWER(`users`.`username`) = LOWER('sean') AND `users`.`id` != 1) LIMIT 1
(0.3ms)  SELECT 1 FROM `users` WHERE (LOWER(`users`.`email`) = LOWER('[email protected]') AND `users`.`id` != 1) LIMIT 1
(0.6ms)  ROLLBACK
 => false 
like image 567
chief Avatar asked Dec 02 '22 01:12

chief


2 Answers

Try this to see if errors are being thrown

@user.save!

If your @user.save is returning false then it means there's an error and something is preventing it from being saved. You can use @user.errors at that point to see what the errors are.

like image 193
Dty Avatar answered Dec 23 '22 21:12

Dty


Had the same issue. Doing this managed to solve it:

@user.save(validate: false)

Here is the link to original answer: How to skip validations as admin during update_attributes?

like image 42
Biwek Avatar answered Dec 23 '22 23:12

Biwek