0

我正在尝试为我的 Sinatra 应用程序构建“夹具”功能。我只是将数据库导出到带有数据库名称的 csv,并使用文件名作为通过 datamapper 访问类的引用。

我知道下面的数据库设置正在工作,因为“.destroy!” 正在工作中。但是我无法通过“.create”方法插入新数据,因为我不断收到“错误数量的参数(1 代表 0)”错误。

我试图手动插入数据(而不是行,实际上是写一个测试哈希),但错误仍然存​​在。

没关系,但我在捆绑环境(bundle exec rake db:fixtures)上通过 rake 运行下面的代码。

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

谢谢!

4

1 回答 1

1

找到了。我的模型之一被称为“队列”,并与 thread.rb 中的队列对象混为一谈。我重命名了我的模型,现在一切都很好。

于 2012-08-08T22:28:43.537 回答