2008年6月10日 星期二

model Object attribute attr_accessor

validate_of_ooxx :email 就只是去用email的mehod去get 而才不管他是table column還是attribute

Object.get_something 也是

所以最常見的就是
attr_accessor :plain_password
validates_presence_of :plain_password, :if => :password_required?
validates_presence_of :plain_password_confirmation, :if => :password_required?
validates_length_of :plain_password, :within => 4..40, :if => :password_required?
validates_confirmation_of :plain_password, :if => :password_required?

def password_required?
password.blank? || !plain_password.blank?
end


----
attr_accessor 可以幫妳做get set
想要overwrite
def plain_password
#this is get method
end

def plain_password=(ooxx)
#this is set method
end

----
>> User.find(856).plain_password
=> nil
>> User.find(856).adfafafd
NoMethodError: undefined method `adfafafd' for #<User:0xb75ac17c>
from /usr/local//lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1860:in `method_missing'
from (irb):13
>>

如果有這個method(attribute?!) 叫了沒東西會是 nil, 可是如果是沒這method(attribute?!)就直接有exception

沒有留言: