3
我正在使用faker生成示例數據。我有這個如下:Rails 3:生成僞造數據以填充DB
require 'faker'
namespace :db do
desc "Fill database with sample data"
task :populate => :environment do
Rake::Task['db:reset'].invoke
User.create!(:name => "rails",
:email => "[email protected]",
:password => "foobar",
:password_confirmation => "foobar")
99.times do |n|
#name = Faker::Name.name
name = "rails#{n+1}"
email = "example-#{n+1}@railstutorial.org"
password = "password"
user = User.create!(:name => name,
:email => email,
:password => password,
:password_confirmation => password)
end
end
end
的問題是,我有一對夫婦在創建用戶時不被稱爲after_save的回調。這是爲什麼?由於
的方法:
after_save :create_profile
def create_profile
self.build_profile()
end
推薦您張貼在模型中after_save的回調部分(包括方法和回調) – 2011-01-13 20:32:06
你想,而不是使用觸發器,覆蓋保存方法?我是新來的鐵軌,所以我不知道這是否是一種反模式練習...... :) – 2011-01-13 20:41:58