0

当我从 CSV 文件创建记录时,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

1

这是你的问题。从模型中删除所有attr_*行。默认情况下可以访问Record所有数据库字段。ActiveRecord通过添加attr字段,您实际上覆盖了默认的 Rails 访问器,这就是为什么一切都表现得很奇怪的原因。

经验教训:当你认为你的模型中没有“有趣的东西”时......要非常怀疑:) 祝你好运。删除这些行应该可以解决所有问题。

于 2011-07-29T20:25:41.360 回答
0

原来是编码问题。

电子邮件、名字和姓氏作为 blob 被放入 SQLite,我猜这会导致一两个问题。添加:encoding => 'u'作为CSV.foreach修复所有内容的选项。

于 2011-07-30T01:46:24.400 回答