简体   繁体   中英

ROR Error message

 @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] 
}

If any ih is coming nil, I should come out of the loop and I need to throw an error message.

I feel like I'm missing something here but it looks like you just need to do :

if ih.nil?
  raise 'some error'
end

You could create a custom exception class (app/models/my_exception.rb):

class MyException < Exception
end

Then:

if ih.nil?
  raise MyException.new
end

You could then handle that error in application_controller if you want to recover from the error gracefully in your rails app:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM