ROR错误消息

时间:2012-01-23 11:07:04

标签: ruby-on-rails

 @supplier_store_array = PurchaseIndentDetail.find(:all,:conditions => 
 "pid.recommended_for = 'PO' and pid.purchase_order_detail_id is null and
 pi.approved_status='Approved' and (pi.amendment_status = 'Approved' or
 pi.amendment_status is null or pi.amendment_status = 'None') and i.is_asset = '0'",
 :joins => "as pid inner join purchase_indents as pi on 
 pi.id=pid.purchase_indent_id inner join items as i on pid.item_id=i.id",
 :select => "pid.*").inject([]){|acc,pid| 

 ih = pid.item.item_subtype.item_type.item_group.store_category.inventory_heads
     .find(:first, :conditions => ["location_id = ?", 
     Location.find(:first, :conditions => ["is_central = true"]).id])

 item_inventory_head_id = ih ? ih.id : pid.item.item_subtype.item_type.item_group
                                      .store_category.inventory_heads[0].id


 acc.include?([pid.supplier_id, item_inventory_head_id]) ? 
 acc : acc << [pid.supplier_id, item_inventory_head_id] 
}

如果任何ih即将出现,我应该退出循环,我需要抛出错误信息。

1 个答案:

答案 0 :(得分:2)

我觉得我在这里遗漏了一些东西,但看起来你只需要这样做:

if ih.nil?
  raise 'some error'
end

您可以创建自定义异常类(app / models / my_exception.rb):

class MyException < Exception
end

然后:

if ih.nil?
  raise MyException.new
end

如果你想在rails app中优雅地从错误中恢复,你可以在application_controller中处理这个错误:

rescue_from MyException do
  render :template => 'my_exception_happened'
end