【发布时间】:2015-06-16 08:48:08
【问题描述】:
我正在通过 CSV 将一些数据导入我的 Rails 应用程序。导入工作正常,但是当我尝试将 contact_name 插入接受的属性时,出现错误:
酒店的未知属性“contact_name”
此字段不属于酒店,但如果它存在于电子表格中,我需要在导入期间创建一个新联系人并将其分配给该酒店。
def self.import(file)
allowed_attributes = [ "id", "status", "rooms", "country_code", "user_id", "hotel_title","hotel_location", "telephone", "email", "contract_date", "print_date", "created_at","updated_at"]
spreadsheet = open_spreadsheet(file)
header = spreadsheet.row(1)
(2..spreadsheet.last_row).each do |i|
row = Hash[[header, spreadsheet.row(i)].transpose]
hotel = find_by_id(row["id"]) || new
hotel.attributes = row.to_hash.select { |k,v| allowed_attributes.include? k }
# ASSIGNING VARIABLES
hotel_name = row["hotel_title"]
hotel.map = Map.create(:hotel_id => row["id"], :status => 0, :title => "Map Publication for #{hotel_name}")
hotel.app = App.create(:hotel_id => row["id"], :status => 0, :title => "Bespoke App for #{hotel_name}")
hotel.save!
end
end
为简单起见,我的 CSV 目前有 4 列。 hotel_title、hotel_location、country_code 和 contact_name。
【问题讨论】:
-
Contact和Hotel是什么关系?
标签: ruby-on-rails ruby csv import-from-excel