当我从CSV文件创建记录时,Activerecord无法使用Record#find_by
找到它们。如果我通过rails控制台创建一个Record,它会按预期工作。这是我通过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输出
Record.find_by_email Record.find(14).email
生成的SQL:
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
答案 0 :(得分:1)
这是你的问题。从attr_*
模型中删除所有Record
行。默认情况下,可以访问所有ActiveRecord
个数据库字段。通过添加attr
字段,您实际上会覆盖默认的Rails访问器,这就是为什么一切都表现得很奇怪。
要学习的课程:当你认为你的模型中“没什么有趣的”时......非常可疑:)祝你好运。删除这些行应该可以解决所有问题。
答案 1 :(得分:0)
原来是一个编码问题。
电子邮件,名字和姓氏作为blob被抛入SQLite,我猜这会导致一两个问题。添加:encoding => 'u'
作为CSV.foreach
的选项可以解决所有问题。