我正在尝试为我的Sinatra应用程序构建一个"固定装置"功能。我只是使用数据库的名称将数据库导出到 csv,并使用文件名作为引用通过数据映射器访问类。
我知道下面的数据库设置正在工作,因为".destroy!"正在工作。但是我无法通过".create"方法插入新数据,因为我不断收到"参数数量错误(1 表示 0)"错误。
我尝试手动插入数据(而不是行,实际上是编写测试哈希),但错误仍然存在。
并不是说这应该很重要,但我正在捆绑环境中通过 rake 运行代码(捆绑执行 rake db:fixtures)。
require 'data_mapper'
require 'config/database'
Dir["./models/fixtures/*.csv"].each do |file|
class_name = DataMapper::Inflector.singularize(
File.basename(file, '.csv').capitalize
)
current_model = Object.const_get(class_name)
current_model.destroy!
# Next 4 lines are turning the file CSV into a Hash of arrays,
# so it can be used to perform the insert on the database.
csv_data = CSV.read(file)
headers = csv_data.shift.map {|i| i.to_sym }
string_data = csv_data.map {|row| row.map {|cell| cell.to_s } }
array_of_hashes = string_data.map {|row| Hash[*headers.zip(row).flatten] }
current_model.transaction do
array_of_hashes.each do |row|
current_model.create(row)
end
end
end
这是执行的 --trace 输出:
** Invoke db:fixtures (first_time)
** Execute db:fixtures
~ default: begin
~ default: rollback
rake aborted!
wrong number of arguments (1 for 0)
/Users/username/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/thread.rb:144:in initialize'
/Volumes/StaticData/fixtures/vendor/bundler/ruby/1.9.1/gems/dm-validations-1.2.0/lib/dm-validations.rb:129:innew'
/Volumes/StaticData/fixtures/vendor/bundler/ruby/1.9.1/gems/dm-validations-1.2.0/lib/dm-validations.rb:129:in create'
/Volumes/StaticData/fixtures/Rakefile:27:inblock (5 levels) in '
/Volumes/StaticData/fixtures/Rakefile:26:in each'
/Volumes/StaticData/fixtures/Rakefile:26:inblock (4 levels) in '
/Volumes/StaticData/fixtures/vendor/bundler/ruby/1.9.1/gems/dm-transactions-1.2.0/lib/dm-transactions.rb:373:in block in transaction'
/Volumes/StaticData/fixtures/vendor/bundler/ruby/1.9.1/gems/dm-transactions-1.2.0/lib/dm-transactions.rb:131:inblock in commit'
/Volumes/StaticData/fixtures/vendor/bundler/ruby/1.9.1/gems/dm-transactions-1.2.0/lib/dm-transactions.rb:195:in within'
/Volumes/StaticData/fixtures/vendor/bundler/ruby/1.9.1/gems/dm-transactions-1.2.0/lib/dm-transactions.rb:131:incommit'
/Volumes/StaticData/fixtures/vendor/bundler/ruby/1.9.1/gems/dm-transactions-1.2.0/lib/dm-transactions.rb:373:in transaction'
/Volumes/StaticData/fixtures/Rakefile:25:inblock (3 levels) in '
/Volumes/StaticData/fixtures/Rakefile:14:in each'
/Volumes/StaticData/fixtures/Rakefile:14:inblock (2 levels) in '
/Volumes/StaticData/fixtures/vendor/bundler/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/task.rb:205:in call'
/Volumes/StaticData/fixtures/vendor/bundler/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/task.rb:205:inblock in execute'
/Volumes/StaticData/fixtures/vendor/bundler/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/task.rb:200:in each'
/Volumes/StaticData/fixtures/vendor/bundler/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/task.rb:200:inexecute'
/Volumes/StaticData/fixtures/vendor/bundler/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/task.rb:158:in block in invoke_with_call_chain'
/Users/username/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/monitor.rb:211:inmon_synchronize'
/Volumes/StaticData/fixtures/vendor/bundler/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/task.rb:151:in invoke_with_call_chain'
/Volumes/StaticData/fixtures/vendor/bundler/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/task.rb:144:ininvoke'
/Volumes/StaticData/fixtures/vendor/bundler/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:116:in invoke_task'
/Volumes/StaticData/fixtures/vendor/bundler/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:94:inblock (2 levels) in top_level'
/Volumes/StaticData/fixtures/vendor/bundler/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:94:in each'
/Volumes/StaticData/fixtures/vendor/bundler/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:94:inblock in top_level'
/Volumes/StaticData/fixtures/vendor/bundler/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:133:in standard_exception_handling'
/Volumes/StaticData/fixtures/vendor/bundler/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:88:intop_level'
/Volumes/StaticData/fixtures/vendor/bundler/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:66:in block in run'
/Volumes/StaticData/fixtures/vendor/bundler/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:133:instandard_exception_handling'
/Volumes/StaticData/fixtures/vendor/bundler/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:63:in run'
/Volumes/StaticData/fixtures/vendor/bundler/ruby/1.9.1/gems/rake-0.9.2.2/bin/rake:33:in'
/Volumes/StaticData/fixtures/vendor/bundler/ruby/1.9.1/bin/rake:23:in load'
/Volumes/StaticData/fixtures/vendor/bundler/ruby/1.9.1/bin/rake:23:in'
Tasks: TOP => db:fixtures
谢谢!
找到了。我的一个模型被称为"队列",它与thread.rb上的队列对象混为一谈。我重命名了我的模型,现在一切都很好。