ActiveRecord从XLS插入数据(脚本已经开发,需要帮助才能在rails env中运行)

时间:2012-05-28 10:42:39

标签: ruby-on-rails ruby ruby-on-rails-3 activerecord

我需要使用访问我使用Rails创建的新应用程序来从旧应用程序中推送一些数据。我已将Access数据导出到.xls,使用gem roo访问它。

它工作得非常好,我创建了一个脚本并打印结果我确信它完全正常,现在我想让它自动将这些数据插入到数据库中,访问rails应用程序模型。

我尝试将文件放在我的rails'lib/目录下,然后我在environments/development.rb中需要它,但是当我导入ruby文件时,它声称无法找到我需要做的一些变量处理

为了完整性,这是我开发的用于从xls中提取数据并编辑它以匹配新数据库模式的完整源代码。

# encoding: utf-8

require 'rubygems'
require 'roo'

user_id_conversion = {119 => 10, 152 => 11, 145 => 12, 161 => 13, 163 => 14, 158 => 15}
conversion_hours = {15 => 14, 17 => 16, 19 => 18}
hostess_id_conversion = {139 => 9, 157 => 17, 153 => 18, 110 => 20, 40 => 19, 141 => 21, 121 => 22, 151 => 23}
status_conversion = {1 => "Inserito", 2 => "Rifiutato", 3 => "Visitato", 4 => "Inserito", 5 => "Ricontattare", 6 => "Vendita"}
mall_id_conversion = {51 => 1, 56 => 2, 53 => 3, 54 => 4, 59 => 5, 65 => 6}

xls = Excel.new("/home/lex/Scrivania/appointments.xls")
xls.default_sheet = xls.sheets.first

city_list = []

# change this to current row numbers
1.upto(3829) do |line|
  uid        = xls.cell(line,'B').to_i

  if user_id_conversion.has_key? uid
    id         = xls.cell(line, 'A')
    uid        = user_id_conversion[uid]
    name       = xls.cell(line, 'F')
    surname    = xls.cell(line, 'E')
    city       = xls.cell(line, 'O')
    street     = xls.cell(line, 'G')
    start_at   = xls.cell(line, 'C')
    start_time = xls.cell(line, 'D')/60/60
    street_no  = xls.cell(line, 'M')
    telephone  = xls.cell(line, 'H')
    mobile     = xls.cell(line, 'I')
    email      = xls.cell(line, 'J')
    notes      = xls.cell(line, 'L')
    agent_note = xls.cell(line, 'U')
    status     = xls.cell(line, 'S')
    signed_by  = xls.cell(line, 'P')
    mall       = xls.cell(line, 'T')

    # we only want to move May -> future
    if start_at.month >= Date.today.month
      ##
      # COMPATIBILITY MODE
      ##
      if conversion_hours.has_key? start_time
        compatibility = true
      else
        compatibility = false
      end

      ##
      # WHO SIGNED THAT APPOINTMENT?
      ##
      if hostess_id_conversion.has_key? signed_by
        signed_by = hostess_id_conversion[signed_by]
      elsif user_id_conversion.has_key? signed_by
        signed_by = user_id_conversion[signed_by]
      else
        signed_by = -1 # display "user deleted"
      end

      ##
      # APPOINTMENT OR EXCLUDED FIELD?
      ##
      available = true
      if status == 4
        available = false
      end
      status = status_conversion[status]

      ##
      # todo SELECT CITY ID FROM DATABASE
      ##
      # city = Municipality.find_by_name(city)
      #

      ##
      # CONVERT MALL_ID
      ##
      if mall_id_conversion.has_key? mall
        mall = mall_id_conversion[mall]
      else
        mall = nil
      end

      ##
      # EMAIL is REQUIRED
      ##
      no_email = false
      if email.blank?
        no_email = true
      end

      ##
      # CONVERT APPOINTMENT DATES TO NEW ONES
      ##
      if compatibility
        old_start_at = DateTime.new start_at.year, start_at.month, start_at.day, start_time
        start_at     = DateTime.new start_at.year, start_at.month, start_at.day, conversion_hours[start_time]
      else
        old_start_at = nil
        start_at     = DateTime.new start_at.year, start_at.month, start_at.day, start_time
      end

      ##
      # todo interesting products
      #    serramenti: boolean, tende_sole: boolean, tecnic: boolean,
      #    tende_veranda: boolean, porte_interne: boolean, tapparelle: boolean, blindati: boolean,
      #    zanzariere: boolean,
      ##

      ##
      # todo INSERT THIS APPOINTMENT
      ##
=begin
      if available == false
        Event.new(
          :event_calendar_id => User.find(uid).event_calendar,
          :status => "Inserito",
          :available => false
          )
      else
        Event.new(
          :event_calendar_id => User.find(uid).event_calendar,
          :signed_by_id => User.find_by_id(signed_by),
          :mall_id => Mall.find_by_id(mall),
          :status => status,
          :name => name,
          :surname => surname,
          :street => street,
          :street_no => street_no,
          :city_id => city.name,
          :telephone => telephone,
          :mobile => mobile,
          :email => email,
          :no_email => no_email,
          :inserting_notes => notes,
          :seller_notes => agent_note,
          :start_at => start_at,
          :available => true,
          group_id: 3,
          :compatibility_start_at => old_start_at
          )
      end
=end

    end
  end
end

如何在rails环境中完成这项工作? 我想在rails c控制台中运行类似于类或函数的东西,因为我今晚需要运行它来获取最新的数据库版本,提取带有更新记录的xls并执行魔法。

很抱歉,如果这似乎是一个noob问题,但我是Ruby的新手。

P.S。我正在使用Rails 3.2.3 w / Ruby 1.9.3 P.P.S.我在Gemfile中插入'roo',因为它首先没有找到宝石。

感谢。

1 个答案:

答案 0 :(得分:1)

如果你这样做

rails runner path/to/script

然后rails将加载rails环境,然后运行脚本