0

因此,我正在开发一个 Ruby on Rails 网站来将 Excel 文件导入我们的网站。我关注RailsCasts并使用Roo gem。一切都在工作,但是当导入过程完成后,所有字段都为零。当我检查时raise,我发现数据为零。

def self.import(file)
    spreadsheet = open_spreadsheet(file)
    header = spreadsheet.row(1)
    (2..spreadsheet.last_row).each do |i|
      row = Hash[[header, spreadsheet.row(i)].transpose] 
      product = find_by_id(row["id"]) || new 
      product.attributes = row.to_hash.slice(*accessible_attributes)
      product.save!
    end
  end

当我提出时row,我从 Excel 中获得了全部数据。但是当我提出时product,我得到了零。如何正确放置数据以便正确获取所有值?谢谢你。

4

1 回答 1

0

所以,在做了一些实践之后,我发现在第一次测试时,它是一个哈希,所以我需要让它工作实际上很简单,但我不确定这是否是最佳实践。如果有人有更好的做法,那将是一件好事,但这是我现在的解决方案。

object = Lead.new(
        :first_name => row['First Name'], :last_name => row['Last Name'],
        :address => row['Address'], :city => row['City'],
        :state => row['State'], :county => row['County'],
        :zip_code => row['Zip Code'], :bedrooms => row['Bedrooms'], :bathrooms => row['Bathrooms'],
        :last_sale_date => row['Last Sale Date'], :amount_sold => row['Amount Sold'],
        :tax_value => row['Tax Value'], :tax_name => row['Tax Name'], :tax_address => row['Tax Address'],
        :tax_city => row['Tax City'], :tax_state => row['Tax State'], :tax_postal_code => row['Tax Postal Code'],
        :listing_status => row['Listing Status'], :property_type => row['Property Type'],
        :square_footage => row['Square Footage'], :days_on_market => row['Days on Market'], :list_date => row['List Date'], 
        :status_change_date => row['Status Change Date'], :year_built => row['Year Built'],
        :mls_id => row['MLS ID'], :last_call_result => row['Last Call Result'], :last_dial_date => row['Last Dial Date'],
        :last_contacted => row['Last Contacted'], :last_dial_time => row['Last Dial Time'], :create_date => row['Create Date'],
        :edit_date => row['Edit Date'], :source => row['Source'], :list => row['List'],
        :call_attempts => row['Call Attempts'], :family_member => row['Family Member'], :notes => row['Notes'],
        :group => row['Group'], :manager => row['Manager']
        )
于 2017-12-31T06:00:17.240 回答