Attr_protected only for updates?

I want to be able to protect the email field of the account from updating, but not when the account entry is created first.

I tried the following:

validate :email_is_unchanged, :on => :update def email_is_unchanged errors.add :email, "can only be changed through confirmation" if email_changed? end 

but when I try to do the following (with an existing entry in the database):

a = Account.first

a.update_attributes ({: email => " Email@example.com ")}

Returns true, but does not save the record. Error checking indicates that an error has been added from the verification method.

Is there a better way to do this?

+4
source share
1 answer

Try the following:

 class Account < ActiveRecord::Base attr_readonly :email end 

This allows you to create new entries by email, but not a subsequent update.

+2
source

All Articles