我不知道如何在模型中保存信息之前初始化信息。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