2012-01-23 117 views
0
@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即将到来零,我应该走出循环,我需要抛出一个错误消息。ROR错误信息

回答

2

我觉得我失去了一些东西,但它看起来像你只需要做:

if ih.nil? 
    raise 'some error' 
end 

您可以创建一个自定义异常类(应用程序/模型/ my_exception.rb):

class MyException < Exception 
end 

然后:

if ih.nil? 
    raise MyException.new 
end 

如果要恢复然后,您可以处理这种错误application_controller从错误的优雅在你的rails应用程序:

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

if i.nil? raise MyException.new end – vijikumar