0

CSV ファイルから Records を作成すると、Activerecord は でそれらを見つけることができませんRecord#find_by。Rails コンソールからレコードを作成すると、期待どおりに動作します。CSV を使用して作成するための私のコードは次のとおりです。

def create
  file = params[:record][:file].tempfile

  CSV.foreach file, :headers => true, :header_converters => :symbol do |row|
    Record.create row.to_hash
  end

  redirect_to records_url
end

コンソールからのいくつかの例を次に示します。

> Record.find_by_email "matching@asd.asd" 
=> nil     # Should return record created through the CSV file

> Record.create :email => "matching@asd.asd"
> Record.find_by_email "matching@asd.asd"
=> <Record id: 4, email: "matching@asd.asd">
> Record.find_all_by_email "matching@asd.asd"
=> [<Record id: 4, email: "matching@asd.asd">]    # Should return both

どんな助けでも大歓迎です。問題があれば、Rails 3.1rc5 を使用しています。

-

rows.to_hash出力で更新

要求に応じて、rows.to_hash のデバッグからの出力:

{:email=>"Matching@asd.asd", :first_name=>"First", :last_name=>"Last"}
{:email=>"Notmatching@asd.asd", :first_name=>"Matching", :last_name=>"Name"}
{:email=>"asdasdasdasd@asd.asd", :first_name=>"asd", :last_name=>"asd"}

コンソールからの別の例は、私の混乱を助長します:

> Record.find 14    # Record created by CSV
=> <Record id: 14, first_name: "First", last_name: "Last", email: "Matching@asd.asd", created_at: "2011-07-29 18:03:25", updated_at: "2011-07-29 18:03:25">
> Record.find(14).email
=> "Matching@asd.asd"
> Record.find_by_email Record.find(14).email
=> nil

-

SQL出力で更新

によって生成された SQL Record.find_by_email Record.find(14).email:

Record Load (0.3ms)  SELECT "records".* FROM "records" WHERE "records"."id" = ? LIMIT 1  [["id", 14]]
Record Load (0.3ms)  SELECT "records".* FROM "records" WHERE "records"."email" = 'Matching@asd.asd' LIMIT 1

-

SQLite コンソールを試す

sqlite> SELECT "records".* FROM "records" WHERE "records"."email" = 'Matching@asd.asd' LIMIT 1;
# This one should have returned the CSV record, but it returns nothing
sqlite> SELECT "records".* FROM "records" WHERE "records"."email" = 'nomatch@asd.asd' LIMIT 1;
5|2|match|this|nomatch@asd.asd|2011-07-29 17:13:13.821972|2011-07-29 17:13:13.821972

-

モデルコードの追加、クエリ結果、CSV 入力

おそらく人類に知られている最もエキサイティングなモデル:

class Record < ActiveRecord::Base
  attr_accessible :email, :first_name, :last_name, :file
  attr_accessor :file
end

クエリからのその他の結果:

sqlite> select * from records;
5|2|match|this|nomatch@asd.asd|2011-07-29 17:13:13.821972|2011-07-29 17:13:13.821972
9|3|first|last||2011-07-29 17:56:50.471766|2011-07-29 17:56:50.471766
10|6|first|last||2011-07-29 17:56:54.917432|2011-07-29 17:56:54.917432
17||First|Last|Matching@asd.asd|2011-07-29 19:43:23.843188|2011-07-29 19:43:23.843188
18||Matching|Name|Notmatching@asd.asd|2011-07-29 19:43:23.849001|2011-07-29 19:43:23.849001
19||asd|asd|asdasdasdasd@asd.asd|2011-07-29 19:43:23.852037|2011-07-29 19:43:23.852037

適切な測定のために、テスト CSV ファイルは次のとおりです。

email,first name,last name
Matching@asd.asd,First,Last
Notmatching@asd.asd,Matching,Name
asdasdasdasd@asd.asd,asd,asd
4

2 に答える 2

0

エンコーディングの問題であることが判明しました。

電子メール、名、および姓がブロブとしてSQLiteにスローされましたが、これにより1つか2つの問題が発生すると思います。すべてを修正する:encoding => 'u'ためのオプションとして追加します。CSV.foreach

于 2011-07-30T01:46:24.400 に答える