0
對不起,我覺得我今天有點笨只有當它爲零時,我如何在ActiveRecord對象中設置屬性?
class Mutant < ActiveRecord::Base
attr_accessible :style
before_create :setup_values
private
def setup_values
style = "hardcore" unless style
end
end
我喜歡把這種東西在控制檯像
Mutant.create(:style => "rampage") # expected is Mutant.first.style == "rampage"
Mutant.create # expected is Mutant.last.style == "hardcore but this doesn't work
你想使用self.style = ...這樣做只是風格= X,林心如解釋,作爲一個局部變量,而不是一個方法調用到#style =。 – 2010-09-05 14:41:53