Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用RooGem将从xlsx提取的哈希存储在ruby中的数据库中_Ruby On Rails_Excel_Ruby_Xlsx_Roo - Fatal编程技术网

Ruby on rails 使用RooGem将从xlsx提取的哈希存储在ruby中的数据库中

Ruby on rails 使用RooGem将从xlsx提取的哈希存储在ruby中的数据库中,ruby-on-rails,excel,ruby,xlsx,roo,Ruby On Rails,Excel,Ruby,Xlsx,Roo,很抱歉我的英语不好,还有一些愚蠢的错误,但我从几个月前开始学习Ruby。 我正在尝试使用GN roo gem读取.xlsx文件,然后将我的行存储在数据库中,存储在已经存在的模型中。 这是我的法庭模型: class Court < ApplicationRecord belongs_to :type validates :type_id, presence: true validates :name, presence: true validates :email, pr

很抱歉我的英语不好,还有一些愚蠢的错误,但我从几个月前开始学习Ruby。 我正在尝试使用GN roo gem读取.xlsx文件,然后将我的行存储在数据库中,存储在已经存在的模型中。 这是我的法庭模型:

class Court < ApplicationRecord

  belongs_to :type

  validates :type_id, presence: true
  validates :name, presence: true
  validates :email, presence: true, uniqueness: true
  validates :responsible, presence: true
  validates :address, presence: true
  validates :telephone, presence: true, format: { with: /\A([0-9]*\-?\ ?\/?[0-9]*)\Z/ }

  geocoded_by :address
  after_validation :geocode, :if => :address_changed?

end

# == Schema Information
#
# Table name: courts
#
#  id                  :integer          not null, primary key
#  type_id             :integer
#  name                :string(255)
#  email               :string(255)
#  email_type          :string(255)
#  responsible         :string(255)
#  address             :string(255)
#  telephone           :string(255)
#  latitude            :decimal
#  longitude           :decimal
#  created_at          :datetime         not null
#  updated_at          :datetime         not null
#
在该文件中,我有相同的模型头,实际上,当我启动任务时,我获得了以下输出:

{"Tipo"=>"Tipo", "Nome"=>"Nome", "Indirizzo e-mail"=>"Indirizzo e-mail", "Tipo email"=>"Tipo email", "Responsabile"=>"Responsabile", "Indirizzo"=>"Indirizzo", "Numero Telefonico"=>"Numero Telefonico"}
{"Tipo"=>"AMM", "Nome"=>"Ministero della Giustizia", "Indirizzo e-mail"=>"gabinetto.ministro@giustiziacert.it", "Tipo email"=>"PEC", "Responsabile"=>"Alfonso Bonafede", "Indirizzo"=>"Via Arenula, 70 - 00186 Roma (RM)", "Numero Telefonico"=>"06 68851"}
{"Tipo"=>"AOO", "Nome"=>"Archivio Notarile Distrettuale di Agrigento", "Indirizzo e-mail"=>"archivionotarile.agrigento@giustiziacert.it", "Tipo email"=>"PEC", "Responsabile"=>"Daniela Portera", "Indirizzo"=>"Via S. Vito, 97/103 - 92100 Agrigento (AG)", "Numero Telefonico"=>"09 2220290"}
{"Tipo"=>"AOO", "Nome"=>"Archivio Notarile Distrettuale di Alessandria", "Indirizzo e-mail"=>"archivionotarile.alessandria@giustiziacert.it", "Tipo email"=>"PEC", "Responsabile"=>"Susanna Cesarone Bongiorno", "Indirizzo"=>"Via Ghilini, 42 - 15121 Alessandria (AL)", "Numero Telefonico"=>"01 31254163"}
{"Tipo"=>"AOO", "Nome"=>"Archivio Notarile Distrettuale di Ancona", "Indirizzo e-mail"=>"archivionotarile.ancona@giustiziacert.it", "Tipo email"=>"PEC", "Responsabile"=>"Margherita Regini Santojanni", "Indirizzo"=>"Piazzale Europa, 7 - 60125 Ancona (AN)", "Numero Telefonico"=>"07 12804055"}
{"Tipo"=>"AOO", "Nome"=>"Archivio Notarile Distrettuale di Aosta", "Indirizzo e-mail"=>"archivionotarile.aosta@giustiziacert.it", "Tipo email"=>"PEC", "Responsabile"=>"Antonio Santoro", "Indirizzo"=>"Via Monsignor De Sales, 3 - 11100 Aosta (AO)", "Numero Telefonico"=>"01 65361395"}
{"Tipo"=>"AOO", "Nome"=>"Archivio Notarile Distrettuale di Arezzo", "Indirizzo e-mail"=>"archivionotarile.arezzo@giustiziacert.it", "Tipo email"=>"PEC", "Responsabile"=>"Gianna Baroni Pedone", "Indirizzo"=>"Via Francesco Crispi, 58/4 - 52100 Arezzo (AR)", "Numero Telefonico"=>"05 7523243"}
但在数据库中我无法存储这些数据。
有人能帮我吗?如果我忘了告诉你一些事情,请提前道歉。

我从来没有使用过“roo”gem,只是查看了一下它们的文档和代码。像这样的东西应该可以完成这项工作:

# header_names and attribute_names must be provided in the same order
# since we work with them based on index later on (#fetch_values and #zip)
header_names = ['Tipo', 'Nome', '...']
attribute_names = [:type, :name, :'...']

Court.transaction do
  # use headers: false to not return the headers
  xlsx.parse(headers: false).each do |row|
    values = row.fetch_values(*header_names)
    attributes = attribute_names.zip(values).to_h
    Court.create!(attributes)
  end
end
由于工作表中的标题名称和模型的属性名称不同,因此需要转换返回的行,该行应该是核心哈希实例

您可以使用(缺少键时引发异常)或(缺少键时返回默认值,未设置时为
nil
)检索字段值。然后,将模型的头与值合并,并将整个内容转换回散列

然后使用创建一个
Court
实例并将其保存到数据库中。当回滚时出现问题时,这会引发一个异常。您也可以使用,但这不会引发异常。这意味着无效记录会被默默地吞没,而不会被保存

需要指出的一点是,您当前的输出显示
“Tipo”=>“AMM”
,其中
“AMM”
是我猜测的类型。但是,这不是一个id,因此在尝试保存之前,您可能需要先获取类型id和其他id

大概是这样的:

attributes_list = xlsx.parse(headers: false).map do |row|
  values = row.fetch_values(*header_names)
  attributes = attribute_names.zip(values).to_h
end

type_ids = Type.where(name: attributes_list.pluck(:type).uniq).pluck(:name, :id).to_h
# fetch other ids if needed.

attributes_list.each do |attributes|
  # remove :type and replace with :type_id (using #fetch to detect missing types)
  attributes[:type_id] = type_ids.fetch(attributes.delete(:type))
end

# create and save Court instances from the attributes
Court.transaction do
  attributes_list.each { |attributes| Court.create!(attributes) }
end

谢谢你的回答!是的,我有另一个模型类型:value和:id来存储不同的值。我试过使用你的代码,但如果我写标题,我会遇到一个问题:false。但是如果我把它改成真的,我会让rake流产的!KeyError:key not found:“…”的意思是您自己填写
标题\名称
属性\名称
。我只加入了两个元素作为示例,后面是
“…”
,表示您应该进一步填充它。不应该使用它,因为键
确实不存在于示例数据中。
attributes_list = xlsx.parse(headers: false).map do |row|
  values = row.fetch_values(*header_names)
  attributes = attribute_names.zip(values).to_h
end

type_ids = Type.where(name: attributes_list.pluck(:type).uniq).pluck(:name, :id).to_h
# fetch other ids if needed.

attributes_list.each do |attributes|
  # remove :type and replace with :type_id (using #fetch to detect missing types)
  attributes[:type_id] = type_ids.fetch(attributes.delete(:type))
end

# create and save Court instances from the attributes
Court.transaction do
  attributes_list.each { |attributes| Court.create!(attributes) }
end