In Ruby, how to insert data on a DataMapper model created dynamically?

399 Views Asked by At

I'm trying to build a "fixture" feature for my Sinatra apps. I'm simply exporting databases to csv's with the name of the database, and using the filename as reference to access the class through datamapper.

I know the database setup bellow is working, because the ".destroy!" is working. But I'm unable to insert new data through the ".create" method as I keep getting "wrong number of arguments (1 for 0)" errors.

I tried to insert the data manually (Instead of row, actually write a test hash) but the error persists.

Not that it should matter, but I'm running the code bellow through rake on a bundled environment (bundle exec 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

This is a --trace output of the execution:

** 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

Thank you!

1

There are 1 best solutions below

0
On BEST ANSWER

Found it. One of my models was called "Queue", and was conflating with a Queue object at thread.rb. I renamed my model and all is fine now.