0

I have a User model really close to Michael Hartl's one

class User < ActiveRecord::Base
  before_save { self.email = email.downcase }
  validates :name, presence: true, length: { minimum: 3, maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }
  has_secure_password
  validates :password, length: { minimum: 6 } 
  validates :about, length: { maximum: 300 }

  ...
end

When trying to update_attributes of an user, I can only do so if I also provide a password and its password_confirmation.

Although I understand this is the expected behavior for User creation, there are some User properties that are to be set without its consent, hence his password.

How is one supposed to achieve that ? I looked for something like has_secure_password, on: :create but it doesn't work.

Edit 1

Oddly enough, I can save a single attribute at a time, using update_attribute instead (which is still quiet a pain, I hope we can do better ) !

Edit 2

I forgot to mention that I tried removing has_secure_password when updating and it works great, so it's definitely the culprit here. Is there an option I'm missing out here ? Should I look at its source code ?

Update : Some console excerpts

irb(main):002:0> User.find(1).update_attribute(:confirmed_email, true)
  User Load (0.4ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
   (0.2ms)  begin transaction
   (0.1ms)  commit transaction

irb(main):003:0> User.find(1).update_attributes(:confirmed_email => true)
  User Load (0.4ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
   (0.2ms)  begin transaction
  User Exists (0.5ms)  SELECT 1 AS one FROM "users" WHERE (LOWER("users"."email") = LOWER('[email protected]') AND "users"."id" != 1) LIMIT 1
   (0.1ms)  rollback transaction
=> false

irb(main):004:0> User.find(1).update_attributes(:confirmed_email => true, password: 'NewPassword', password_confirmation: 'NewPassword')
  User Load (0.4ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
   (0.2ms)  begin transaction
  User Exists (0.3ms)  SELECT 1 AS one FROM "users" WHERE (LOWER("users"."email") = LOWER('[email protected]') AND "users"."id" != 1) LIMIT 1
Binary data inserted for `string` type on column `password_digest`
  SQL (0.7ms)  UPDATE "users" SET "password_digest" = ?, "updated_at" = ? WHERE "users"."id" = 1  [["password_digest", "$2a$10$CMxLD91SCHWcdhJ3ciU2jez4Zw.gD7o3JszBuOf0gY04MVap56dGy"], ["updated_at", Mon, 27 Jan 2014 21:38:36 UTC +00:00]]
   (172.4ms)  commit transaction
=> true
4
  • Possible duplicate of stackoverflow.com/questions/20475333/… Commented Jan 27, 2014 at 21:22
  • I tried that and it didn't work; Ijust realized I'm actually a duplicate of stackoverflow.com/questions/11386092/…. Should I delete ? Commented Jan 27, 2014 at 21:25
  • I wouldn't as that approach would require you to do a update_attribute for each field you're trying to update. Seems you might have a different issue on your hands. Just thought it might have been related to the one that I posted. Can we see your controller or any other relevant code? Commented Jan 27, 2014 at 21:28
  • 1
    I can't do it even in the console, so the controller should not be relevant here, right ? Commented Jan 27, 2014 at 21:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.