lambda - Rails scopes with multiple arguments -


in offer.rb model doing filtering after clients have these offers. want able pass param in scope search by, example, age of client or so.

this have in offer model:

scope :with_client_id, lambda { |client_id| where(:client_id => client_id) } 

in view:

<%= f.select(:with_client_id, options_for_select(current_partner.clients.collect{|c| [c.name, c.id]}), { include_blank: true}) %> 

how can pass client's age per in scope?

thanks!

two options

using "splat"

scope :with_client_id_and_age, lambda { |params| where(:client_id => params[0], :age => params[1]) } 

and you'd have call with:

offer.with_client_id_and_age( client_id, age ) 

using params hash

scope :with_client_id_and_age, lambda { |params| where(:client_id => params[:client_id], :age => params[:age]) } 

and you'd have call with:

offer.with_client_id_and_age( { client_id: client_id, age: age } ) 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -