2011-07-10 77 views

回答

6

我還沒有用ActiveRecord試過,但是這樣做有效嗎?

class User < ActiveRecord::Base 
    # stuff and stuff ... 

    def fullname 
    super || email 
    end 
end 

它取決於ActiveRecord在這些方法中的混合程度。

3

做你想做什麼,你可以很容易地覆蓋默認的閱讀器fullname和做這樣的事情:

class User < ActiveRecord::Base 
    def fullname 
    # Because a blank string (ie, '') evaluates to true, we need 
    # to check if the value is blank, rather than relying on a 
    # nil/false value. If you only want to check for pure nil, 
    # the following line wil also work: 
    # 
    # self[:fullname] || email 
    self[:fullname].blank? ? email : self[:fullname] 
    end 
end 
相關問題