Having trouble with this seemingly simple task of importing a CSV file via a Rake task, and storing in multiple tables.
My rake task:
desc "Imports the COGCC CSV file into wikifrac database"
task :import_cogcc => :environment do
require 'csv'
CSV.foreach('public/partial.csv', :headers => true) do |row|
# create records in independent tables
# create the Company object
this_company_name = row['name'].strip!
this_operator_num = row['operator_num']
if !(Companies.exists?(:company_name => this_company_name))
Companies.create(:company_name => this_company_name, :operator_num => this_operator_num)
end
thecompany = Companies.find(:first, :conditions => ["this_company_name = ?", this_company_name])
company_id = thecompany.id
...
My Companies model:
class Companies < ActiveRecord::Base
has_many :facilities, dependent: :destroy
attr_accessor :company_name, :operator_num
def initialize(company_name, operator_num)
@operator_num = operator_num
@company_name = company_name
end
end
But when I run rake import_partial, I get this error:
rake aborted! ArgumentError: wrong number of arguments (1 for 2) wfrails/app/models/companies.rb:5:in
initialize' wfrails/lib/tasks/import_partial.rake:47:in
block (2 levels) in ' wfrails/lib/tasks/import_partial.rake:26:in `block in ' Tasks: TOP => import_cogcc
Can anyone tell me what's wrong with this? Have been going around and around with this; many similar examples in SO, but cannot quite solve my error....