Rails: prevent division by zero with postgres
问题 I have my rails app with a postgre database and I'm trying to get some indicators, with the following statement Negotiation.find(:all, :conditions => conditions_hash('negotiation'), :select => 'sum(payment_term * amount_after_annualised_base) / sum(amount_after_annualised_base) as pt').first how can I make sure that sum(amount_after_annualised_base) is not 0 (thus doesn't throw a "division by zero" error)? I've found this, but not sure it is correct / the best / most efficient way.