Rails 5.1
我的迁移文件:
class CreateFwExports < ActiveRecord::Migration[5.1]
def change
create_table :fw_exports, id: :string do |t|
t.string :screen_name, index: true, limit: 16
t.string :full_name, limit: 21
t.string :location_placeholder
t.timestamps
end
end
end
来自fw_exports控制器:
def fw_export_params
params.require(:fw_export).permit(:screen_name, :full_name, :location_placeholder)
end
在fw_export.rb模型中,我有:
has_one :location
has_many :followers
在location.rb模型中,我有:
belongs_to :fw_export
在follower.rb模型中,我有:
has_many :followed
在我的帮助文件中,我有以下方法:
def process_spreadsheet(number_of_rows, spreadsheet, followed_id)
(1..number_of_rows).each do |i|
fw_export_record = FwExport.new(
:screen_name => spreadsheet[i][0],
:full_name => spreadsheet[i][1],
:location_placeholder => spreadsheet[i][2],
)
fw_export_record.save
location = Location.new(
:fw_exports_id => fw_export_record.id
)
location.save
follower = Follower.new(
:followed_id => followed_id,
:fw_exports_id => fw_export_record.id
)
follower.save
end
end
此方法的作用是接收电子表格CSV对象,并迭代数据,尝试将每行保存到fw_exports表。它还会在位置和关注者表中创建条目
有一个位置表,包含以下字段:
t.string :fw_exports_id, index: true
t.string :city
t.string :state
t.string :country
以及包含以下字段的关注者表:
t.string :followed_id
t.string :fw_exports_id
t.string :slug, index: true
当我尝试导入数据时,出现以下错误:
NoMethodError (undefined method `each' for "0":String):
app/helpers/fw_exports_helper.rb:21:in `block in process_spreadsheet'
app/helpers/fw_exports_helper.rb:20:in `process_spreadsheet'
app/controllers/fw_exports_controller.rb:82:in `process_parsed_spreadsheet'
我验证了传递给process_spreadsheet调用的所有参数都是有效的。
有什么想法吗?