insert_fixtures被弃用或移走
我在看《RailsSpace-Building a Social Networking Website with Ruby on Rails》这本书,在第十章时作者使用了
require 'active_record'
require 'active_record/fixtures'
namespace :db do
DATA_DIRECTORY = "#{Rails.root.to_s}/lib/tasks/sample_data"
namespace :sample_data do
TABLES = %w(users specs faqs)
MIN_USER_ID = 1000 # Starting user id for the sample data
desc "Load sample data"
task :load => :environment do |t|
class_name = nil # Use nil to get Rails to figure out the class.
TABLES.each do |table_name|
fixture = ActiveRecord::Fixtures.new(ActiveRecord::Base.connection,
table_name, class_name,
File.join(DATA_DIRECTORY, table_name.to_s))
#fixture.insert_fixtures
fixture.dclone
puts "Loaded data from #{table_name}.yml"
end
end
desc "Remove sample data"
task :delete => :environment do |t|
User.delete_all("id >= #{MIN_USER_ID}")
Spec.delete_all("user_id >= #{MIN_USER_ID}")
Faq.delete_all( "user_id >= #{MIN_USER_ID}")
end
end
end
这段代码来从yml文件向数据库中加载数据
但我在运行时有这样的错误提示:
undefined method `insert_fixtures' for #<ActiveRecord::Fixtures:0x1d56830>
查了一下,说是insert_fixtures这个函数从rails 3.1.0之后就弃用或移走了。
现在请教下从yml文件加载数据的新方法
[解决办法]
YAML::load_file("#{RAILS_ROOT}/config/xxxx.yml")
[解决办法]