0

我有一个从 excel 导入的功能。我把它放在我的模型上:

def self.import(file, employee_name)
  spreadsheet = open_spreadsheet(file)
  header = spreadsheet.row(1)
  (2..spreadsheet.last_row).each do |i|
    row = Hash[[header, spreadsheet.row(i)].transpose]
    category = Category.where(:name => row["Category"]).last
    if category.blank?
      category = Category.create(:name => row["Category"], :is_active => 1)
    end
    unit = UnitOfMeasure.where(:name => row["Unit"]).last
    if unit.blank?
      unit = UnitOfMeasure.create(:name => row["Unit"], :is_active => 1)
    end

    chart_of_account_id=0
    stock_output_account=0

    if row["Can Sold"]==1
      income_account=1
    else
      income_account=0
    end

    if row["Can Purchased"]==1
      expense_account=1
    else
      expense_account=0
    end

    product = Product.create(:plu => row["PLU"], :plu_night_disc => row["PLU Night Disc."], :name => row["Item Desc."], :min_stock => ["Min. Stock"], :product_type => row["Product Type"], :notes => ["Notes"], :sales_price => ["Sales Price"], :night_disc_price => ["Night Disc. Price"], :bottom_price => ["Bottom Price"], :category_id => category.id, :unit_of_measure_id => unit.id, :chart_of_account_id => chart_of_account_id, :stock_output_account => stock_output_account, :income_account => income_account, :expense_account => expense_account, :can_be_sold => row["Can Sold"], :can_be_purchased => row["Can Purchased"], :employee_name => employee_name, :is_active => 1)

  end
end

但是当我进行导入时,它不会返回任何错误,但我的创建Product只是跳过(查找长代码),当我尝试更改Product示例以Country对其插入数据库进行精细建模时。我确实对这种行为感到困惑。请任何帮助。谢谢

4

1 回答 1

0

您遇到的这种行为可能意味着您的Product记录无效,并且插入正在静默失败。尝试改用该create!方法:

product = Product.create!(...)

如果您的模型无效,此方法将引发错误,并解释原因。您可以使用该信息来调试您的代码。

希望有帮助。

于 2013-03-04T12:05:18.667 回答