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