0

我已经设法使用 Roo gem 通过我的 Rails 5 应用程序解析了一个电子表格,但是一旦它完成了这个过程,它似乎并没有保存到数据库中。

这是我的代码:

require 'roo'

xlsx = Roo::Excelx.new(File.expand_path('../Downloads/LOCATION.xlsx'))

xlsx.each_row_streaming(offset: 1) do |row|

Location.find_or_create_by(ukprn: row[0]&.value, accomurl: row[1]&.value, instbeds: row[3]&.value, instlower: row[4]&.value, instupper: row[5]&.value, locid: row[6]&.value, name: row[7]&.value, lat: row[9]&.value, long: row[10]&.value, locukprn: row[11]&.value, loccountry: row[12]&.value, privatelower: row[13]&.value, privateupper: row[14]&.value, suurl: row[15]&.value)

end

这将遍历具有正确属性的每一行,给出以下结果:

Location Load (0.8ms)  SELECT  "locations".* FROM "locations" WHERE "locations"."ukprn" = $1 AND "locations"."accomurl" = $2 AND "locations"."instbeds" = $3 AND "locations"."instlower" = $4 AND "locations"."instupper" = $5 AND "locations"."locid" = $6 AND "locations"."name" = $7 AND "locations"."lat" = $8 AND "locations"."long" = $9 AND "locations"."locukprn" = $10 AND "locations"."loccountry" = $11 AND "locations"."privatelower" = $12 AND "locations"."privateupper" = $13 AND "locations"."suurl" = $14 LIMIT $15  [["ukprn", "99999998"], ["accomurl", "http://www2.hull.ac.uk/student/accommodation-new/accommodationhull/first-year-students.aspx"], ["instbeds", 3033], ["instlower", 3500], ["instupper", "5200"], ["locid", "HC"], ["name", "The University of Hull"], ["lat", 53.76964], ["long", -0.367165], ["locukprn", 10007149], ["loccountry", "XF"], ["privatelower", 3100], ["privateupper", "3800"], ["suurl", "http://www.hullstudent.com/"], ["LIMIT", 1]]
   (0.4ms)  BEGIN
   (0.3ms)  ROLLBACK

并继续遍历所有行,直到最后一行:

  Location Load (0.6ms)  SELECT  "locations".* FROM "locations" WHERE "locations"."ukprn" = $1 AND "locations"."accomurl" = $2 AND "locations"."instbeds" = $3 AND "locations"."instlower" = $4 AND "locations"."instupper" = $5 AND "locations"."locid" = $6 AND "locations"."name" = $7 AND "locations"."lat" = $8 AND "locations"."long" = $9 AND "locations"."locukprn" = $10 AND "locations"."loccountry" = $11 AND "locations"."privatelower" = $12 AND "locations"."privateupper" = $13 AND "locations"."suurl" = $14 LIMIT $15  [["ukprn", "99999999"], ["accomurl", "http://www.bsms.ac.uk/undergraduate/our-course"], ["instbeds", 5720], ["instlower", 4785], ["instupper", "6011"], ["locid", "KF"], ["name", "Falmer"], ["lat", 50.860268], ["long", -0.085662], ["locukprn", 99999999], ["loccountry", "XF"], ["privatelower", 4977], ["privateupper", "5756"], ["suurl", "http://www.bsms.ac.uk/undergraduate/campus-life/student-life/"], ["LIMIT", 1]]
   (0.8ms)  BEGIN
   (0.4ms)  ROLLBACK
 => nil 

但是,无论出于何种原因,这都不会将数据提交到 postgres 数据库。我不确定这是为什么。

如果我将代码更改为Location.create!(ukprn: row[0]&.value, accomurl: row[1]&.value, instbeds: row[3]&.value, instlower: row[4]&.value, instupper: row[5]&.value, locid: row[6]&.value, name: row[7]&.value, lat: row[9]&.value, long: row[10]&.value, locukprn: row[11]&.value, loccountry: row[12]&.value, privatelower: row[13]&.value, privateupper: row[14]&.value, suurl: row[15]&.value)它会出现以下错误:

(485.6ms)  BEGIN
   (156.0ms)  ROLLBACK
ActiveRecord::RecordInvalid: Validation failed: Institute must exist
    from /home/dave/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/validations.rb:78:in `raise_validation_error'

其中 Institute 是参考(外键)。任何意见,将不胜感激。

4

0 回答 0