2

私はモデル(テストと呼ばれる)を持っています:

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

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

終わり

インポーターを実行してもエラーは表示されません。SQLiteテーブルに何も挿入されていないようです。

明らかな何かが欠けていますか?(テーブルはターゲットデータベース内に存在し、フィールド名は私のファイルのヘッダーと同じです。

4

1 に答える 1

2

アップデート2014/11/19:FasterCSVは削除されました。これで、Ruby標準ライブラリCSVを代わりに使用する必要があります。FasterCSVのすべての出現をで置き換えるだけですCSV

私が推測する2つの問題があります

  • 使用する予定の区切り文字は、「/t」ではなく「\t」でした
  • row_dataを使用してdatamapperオブジェクトにデータを入力していません

これはうまくいくはずです:

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
于 2010-09-27T21:57:06.603 に答える