Matt Jones wrote in post #1154197:
> On Saturday, 2 August 2014 11:41:46 UTC-4, Ruby-Forum.com User wrote:
>> > method.
>> because I often have the case that I will update only some of the
>> attributes. I wonder *why* validates looks at attributes which are not
>> part of the update. Is there a use case where this makes sense?
>>
>>
> This is usually the desired behavior, because you want to ensure that
> the
> *whole* record is valid before saving.
Well, I am reading the record from the database, and only update some
fields, so I thought the remaining fields should be treated as
consistent.
>
> :password is a outlier here, since it isn't persisted to the database.
Maybe this is the reason why the problem shows up here. I thought that
the record already has the password_digest, so it would be fine. :-(
> In
> your case, I might add something like:
>
> validates :password, length: { minimum: 6 }, on: :create
>
> Since (unless users can change their passwords) you only need to check
> it
> when creating a new record.
That's exactly the problem: The user should be able to change the
password, but if he only changes one of the other fields, he should not
need to supply the password (since he is already logged in anyway). I
implemented this by providing an entry form with empty password (and
empty password confirmation). From the response, I remove both password
fields from the params hash, if both are empty, and I also remove the
user id, if it has not changed (because there is an index on it, and I
don't want to trigger unnecessary index update operations). After this,
I pass the params to update_attributes.
When it comes to updating profile information, I had expected that this
would be the "normal" way to do it. Am I wrong in this respect?