0

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

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

我尝试将文件放在我的 railslib/目录下,然后我需要它environments/development.rb,但是当我导入 ruby​​ 文件时,它声称找不到一些我需要进行处理的变量。

为了完整起见,这是我开发的用于从 xls 中提取数据并对其进行编辑以匹配新的 db 模式的整个源代码。

# 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 并施展魔法。

抱歉,这似乎是一个菜鸟问题,但我是 Ruby 新手。

PS 我正在使用带有 Ruby 1.9.3 PPS 的 Rails 3.2.3 我在 Gemfile 中插入了“roo”,因为它首先没有找到 gem。

谢谢。

4

1 回答 1

1

如果你这样做

rails runner path/to/script

然后rails会加载rails环境,然后运行你的脚本

于 2012-05-28T10:53:54.833 回答