2

I have a model (called Test):

property :id,           Serial  
property :title,        String,     :length => 255, :required => true
property :description,  String,     :length => 255, :required => true
property :brand,        String,     :length => 255, :required => true
property :link,         String,     :length => 255, :required => true
property :image_link,   String,     :length => 255, :required => true
property :price,        String,     :length => 255, :required => true
property :condition,    String,     :length => 255, :required => true
property :product_type, String,     :length => 255, :required => true

I am importing data from a tab delimited file, using FasterCSV,

FasterCSV.foreach("test.txt", {:headers => true, :quote_char=>'"', :col_sep =>'/t'}) do |row_data|

 row_data = Test.first_or_new(
    'title' =>  :title,
    'description' => :supplier,
    'brand' => :brand,
    'link' => :link,
    'image_link' => :image_link,
    'price' => :price,
    'condition' => :condition,
    'product_type' => :product_type
  )

row_data.save

end

No errors appear, when I run the importer. Nothing appears inserted in SQLite table.

Am i missing something obvious? (The table exists within the target database, and the field names are the same as the headers from my file.

1 Answer 1

2

Update 2014/11/19: FasterCSV has been removed. Ruby standard library CSV should now be used intead. Just replace all occurrences of FasterCSV with CSV

There's two problem i guess

  • the delimiter you intended to use was rather "\t" than '/t'
  • you're not using the row_data to populate the datamapper object

This should work better:

FasterCSV.foreach("test.txt", {:headers => true, :quote_char=>'"', :col_sep =>"\t"}) do |row_data|

    new_record = Test.first_or_new(
        'title' =>  row_data['title'],
        'description' => row_data['supplier'],
        'brand' => row_data['brand'],
        'link' => row_data['link'],
        'image_link' => row_data['image_link'],
        'price' => row_data['price'],
        'condition' => row_data['condition'],
        'product_type' => row_data['product_type']
    )
    new_record.save
end
9
  • Thanks, I've used your version, with the correct tab delimiter and using the correct way to find the items from the row_data. Though still not getting anything importing. Anyway to get FasterCSV to produce logs, or would it error if data was wrong?
    – Neil
    Sep 27, 2010 at 22:08
  • yes sure, you can add a "puts row_data.inspect" right after the foreach line in order to trace what the row_data contains. (by the way the "\t" means that your fields are tab separated, is it ok?)
    – hellvinz
    Sep 27, 2010 at 22:12
  • Correct it is tab delimited, an example row output from FasterCSV confirms it is assigning the right values to each column when reading. Just the save method seems to not be hitting into DataMapper.
    – Neil
    Sep 27, 2010 at 22:15
  • since all fields seems to be required, it may fail because the instance is not valid. Are you sure that they are all filled (check the valid? method)
    – hellvinz
    Sep 28, 2010 at 7:34
  • OK I changed the model to allow nulls, and just import the title. SQLite table reflects just an ID and Title. Now receiving an error: 'tests.description may not be NULL (DataObjects::IntegrityError)'
    – Neil
    Sep 28, 2010 at 12:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.