0
我爲我的Ruby應用程序設置了CSV導入。一切工作都很好,但是當我嘗試上傳一個字符串,然後搜索一個id作爲外鍵輸入到哈希中時,我遇到了問題。我的CSV模型看起來如下:用於外鍵的Rails CSV導入切換字符串
class Player < ActiveRecord::Base
belongs_to :team
validates :team_id, presence: true
def self.import(file)
CSV.foreach(file.path, headers: true, :header_converters => :symbol) do |row|
player_hash = row.to_hash
teamname = player_hash[:team]
teamhash = Team.where(:name => teamname).first
hashid = teamhash.id
player_newhash = player_hash.reject!{ |k| k == :team}
player_newhash[:team_id] = hashid
end
Player.create! (player_newhash)
end
end
我確信這是問題所在。當試圖執行我的錯誤:
未定義的局部變量或方法`player_newhash」爲#
任何幫助將不勝感激。