我在Rails中有一个股票价格历史模型,我想将这些数据从yahoo finance api批量导入到我的模型中。
这就是我所拥有的:
columns = [:date,:open,:high,:low,:close,:volume,:adjusted_close,:asset_symbol]
values = ['2013-03-22',1,2,3,4,5,6,'YHOO']
AssetHistory.import columns,values这会导致一个错误:undefined method each_with_index for '2013-03-22':String from .../activerecord-import/import.rb
请注意,我的值数组中的日期实际上是我的模型中的date类型。(这就是问题所在吗?)
我使用的是Activerecord-import 0.3.1和Rails 3.2。如果有必要的话,我也在使用Postgresql。
我的带注释的资产历史模型:
# Table name: asset_histories
#
# id :integer not null, primary key
# date :date
# open :float
# high :float
# low :float
# close :float
# volume :integer
# adjusted_close :float
# asset_symbol :string(255)
# asset_id :integer
#
class AssetHistory < ActiveRecord::Base
attr_accessible :adjusted_close, :close, :date, :high, :low, :open, :volume, :asset_id, :asset_symbol
has_and_belongs_to_many :assets
validates_uniqueness_of :asset_symbol, scope: [:date]
end这个问题有什么原因吗?
发布于 2013-03-23 03:52:08
在将对象导入到模型中时,我们应该像下面这样调用该方法。
Model.import <array_of_cols>, <array_of_values>例如:
Sample.import [:field1, :field2], [["value-a1", "value-a2"], ["value-b1", "value-b2"]]上面的示例将创建示例模型的两条记录。
因此,在您的情况下,请像这样提供。
AssetHistory.import columns, [values]希望这能行得通。
https://stackoverflow.com/questions/15577830
复制相似问题