我试图在 Rails 3 中使用 after_initialize 来自动构建某人的第一篇文章。我在模型中设置了默认值,但我希望一旦创建用户,它就会构建他们的第一个帖子。因此,他们将有一个 ID 为 1 的 Post 条目,而不只是 default_values。
模型:
class Etho < ActiveRecord::Base
belongs_to :user
attr_accessible :word_one, :word_two, :word_three, :word_four, :tagline
after_initialize :default_values
private
def default_values
self.word_one ||= "Adventagious"
self.word_two ||= "Funny"
self.word_three ||= "Multidisciplined"
self.tagline ||= "Shares the same visions as JFK. Wants to see the world fortune in prosperity."
end
end