开发者

attr_protected only for updates?

I want to be able to protect the email field of an Account from being updated, but not when the Account record is first created.

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 exis开发者_Go百科ting record in the database):

a = Account.first

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

It returns true but doesn't save the record. Inspection of the errors shows that the error from the validation method was added.

Is there a better way to do this?


Try the following:

class Account < ActiveRecord::Base
    attr_readonly :email
end

This allows creation of new records with an email, but not subsequent update.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜