2012-06-02 21 views

回答

1

是的。

class User 
    attr_protected :email 
end 

這裏是你如何使用它:

user = User.new(params[:user]) 
user.email = params[:user][:email].downcase 

如果你想downcase郵件屬性,雖然自動,你可以簡單地覆蓋email=方法,我強烈建議:

class User < ActiveRecord::Base 
    def email=(other) 
    write_attribute(:email, other.try(:downcase))                                       
    end 
end 

Loading development environment (Rails 3.2.5) 
irb(main):001:0> User.new({:email => '[email protected]'}) 
=> #<User id: nil, email: "[email protected]", username: nil, created_at: nil, updated_at: nil> 
irb(main):002:0> User.new({:email => nil}) 
=> #<User id: nil, email: nil, username: nil, created_at: nil, updated_at: nil> 
相關問題