我不知道如何在模型中保存信息之前初始化信息。Ruby - 初始化模型
例如。我有一个名为Car的模型,它具有wheel_size,color等属性... 我想根据每辆新车的其他因素初始化这些属性。
这就是我现在要做的。
Class Car < ActiveRecord::Base
before_save :initial_information
def initial_information
self.color = value1
self.wheel_size = value2
end
end
检查此SO上的帖子:http://stackoverflow.com/questions/328525/what-is-the-best-way-to-set-default-values-in-activerecord – 2010-08-06 18:22:58