Simple question
In rails I have an ApplicationHelper method
def sum_customer_yearly_revenue(customer_id, year)
sum_customer_yearly_revenue = Sale.sum(:net_amount, :conditions => ['customer_id = ? AND financial_year = ?', customer_id, year])
end
In my view I then call sum_customer_yearly_revenue(123456, 2014)
. How do I call the same method but with 'all' years. If I was using SQL it would be sum_customer_yearly_revenue(123456, *) but that returns an error. If it pass in "" it looks for a year that is empty. If I leave it blank it just errors aswell.
def sum_customer_yearly_revenue(customer_id, year="all")
sales = Sale.where(customer_id: customer_id)
sales = sales.where(year: year) if year!="all"
sales.sum(:net_amount)
end
This might be useful to you. In this case if you want to retrieve sum for all year there is no necessary to pass second argument.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments