2017-12-29 22 views
0

私はRuby on Rails Webサイトを開発して、ExcelファイルをWebサイトにインポートしています。私はRailsCastsに続き、Roo gemを使用しました。すべてが機能していますが、インポート処理が完了すると、すべてのフィールドはゼロになります。私がraiseとチェックしたところ、データがゼロであることがわかりました。Ruby on Rails RooインポートExcelリターンNIL

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を育てたとき、私はゼロになった。どのようにデータを正しく置くことができるので、すべての価値を正しく得ることができますか?ありがとうございました。

答えて

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'] 
     )