當我從CSV文件創建記錄時,Activerecord無法通過Record#find_by
找到它們。如果我通過導軌控制檯創建一個記錄,它按預期工作。這是我通過CSV創建代碼:Activerecord未找到使用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 "[email protected]"
=> nil # Should return record created through the CSV file
> Record.create :email => "[email protected]"
> Record.find_by_email "[email protected]"
=> <Record id: 4, email: "[email protected]">
> Record.find_all_by_email "[email protected]"
=> [<Record id: 4, email: "[email protected]">] # Should return both
任何幫助,將不勝感激。如果它很重要,我在Rails 3.1rc5上。
-
與rows.to_hash
輸出更新
按照要求,從調試rows.to_hash輸出:
{:email=>"[email protected]", :first_name=>"First", :last_name=>"Last"}
{:email=>"[email protected]", :first_name=>"Matching", :last_name=>"Name"}
{:email=>"[email protected]", :first_name=>"asd", :last_name=>"asd"}
從控制檯,這進一步我的困惑又如:
> Record.find 14 # Record created by CSV
=> <Record id: 14, first_name: "First", last_name: "Last", email: "[email protected]", created_at: "2011-07-29 18:03:25", updated_at: "2011-07-29 18:03:25">
> Record.find(14).email
=> "[email protected]"
> 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" = '[email protected]' LIMIT 1
-
試行SQLite的控制檯
sqlite> SELECT "records".* FROM "records" WHERE "records"."email" = '[email protected]' LIMIT 1;
# This one should have returned the CSV record, but it returns nothing
sqlite> SELECT "records".* FROM "records" WHERE "records"."email" = '[email protected]' LIMIT 1;
5|2|match|this|[email protected]|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|[email protected]|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|[email protected]|2011-07-29 19:43:23.843188|2011-07-29 19:43:23.843188
18||Matching|Name|[email protected]|2011-07-29 19:43:23.849001|2011-07-29 19:43:23.849001
19||asd|asd|[email protected]|2011-07-29 19:43:23.852037|2011-07-29 19:43:23.852037
爲了更好的衡量,測試CSV文件:
email,first name,last name
[email protected],First,Last
[email protected],Matching,Name
[email protected],asd,asd
請在循環內調試'row.to_hash'包含的內容。 'logger.info row.to_hash'。 – Casper
@Casper:我用'logger.info row.to_hash'和另一個例子的結果編輯了問題。希望能幫助到你。 – Nathan
嗯。添加更多調試到控制檯:'ActiveRecord :: Base.logger = Logger.new(STDOUT)'。它爲'Record.find_by_email Record.find(14).email'生成了什麼SQL? – Casper