0

I ran this in a rails console:

u = User.first
  User Load (0.3ms)  SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1
 => #<User id: 4, name: "Administrator", email: "administrator@wdis.com", created_at: "2013-09-27 01:01:54", updated_at: "2013-09-29 23:52:11", password_digest: ".......", remember_token: "........", admin: true, num_credits: nil> 
2.0.0p247 :002 > u.num_credits = 1
 => 1 
2.0.0p247 :003 > u.num_credits
 => 1 
2.0.0p247 :004 > u.save
   (0.3ms)  begin transaction
  User Exists (0.3ms)  SELECT 1 AS one FROM "users" WHERE (LOWER("users"."email") = LOWER('administrator@wdis.com') AND "users"."id" != 4) LIMIT 1
   (0.2ms)  rollback transaction
 => false 
jackerman09
  • 2,492
  • 5
  • 29
  • 46

1 Answers1

1

There must be some validation which is stopping this user to be saved. Just check your validations in User model. You can also check the errors on this user using u.errors.full_messages.to_sentence

techvineet
  • 5,041
  • 2
  • 30
  • 28
  • Thanks! It is telling me that "password is too short", which I had completely forgotten was a validation for existing users. Is it possible to have a validation that only runs when the password is being changed? This way the controller can change a field on user (in this case num_credits) without having to enter a password? – jackerman09 Oct 01 '13 at 02:00
  • Done, any idea on my followup question? – jackerman09 Oct 01 '13 at 02:04
  • 1
    You can restict the validation to occur on update or create or both but not on attribute change – techvineet Oct 01 '13 at 02:05