Rails/Ruby how to pass 'all' as condition variable

Jay Killeen

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.

jbmyid
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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Query for condition 'ALL' with Variable?

From Dev

How to pass sql variable to like condition SQL Server 2012

From Dev

How to pass variable from controller to all view files (.ctp) in cakephp

From Dev

How to pass a variable which has multiple arguments to function all at once?

From Dev

How to query array elements in mongo so that all nested items pass a given condition?

From Dev

How to dynamically pass condition of if statement

From Java

How to pass closure as a variable?

From Dev

How to pass variable to cpack?

From Dev

How to pass a switch variable?

From Dev

How to pass a variable to a filter

From Dev

How to pass a variable to setInterval?

From Dev

How to Pass Variable into Function

From Dev

How to pass a variable as an argument

From Dev

How to declare a variable inside an if condition?

From Dev

how to add variable into ActiveRecord :condition => ''

From Dev

How to declare a variable inside an if condition?

From Dev

Laravel 5 fails to pass variable to all views

From Dev

Not all threads notified of condition_variable.notify_all()

From Dev

how to pass optional parameter in linq where condition

From Dev

How to pass query directly to the where condition?

From Dev

how to pass optional parameter in linq where condition

From Dev

How to pass attribute ID to ngCLass condition

From Dev

How to pass condition statement as argument to functions in zsh?

From Dev

how to pass variable to -e on gnuplot?

From Dev

How to pass javascript variable in class?

From Dev

How to pass variable to my code

From Dev

How to pass a variable to a server tag?

From Java

How to pass a function value to a variable?

From Dev

How to pass a variable as Converterparameter in WPF

Related Related

HotTag

Archive