How to pass a hash as an argument to a function?

Hi, I want to pass the “params” hash to a function,

@users = User.search(params)

but clearly the below is not the way to do it …

def self.search(*search)
conditions = []
if !search[:user_type_id].is_blank?
conditions << ‘user_type_id = ?’ <<
“#{search[:user_type_id]}”
end
if !search[:ship_to_first_name].is_empty?
conditions << ‘ship_to_first_name LIKE ?’ <<
“%#{search[:ship_to_first_name]}%”
end
if !search[:ship_to_last_name].is_empty?
conditions << ‘ship_to_last_name LIKE ?’ <<
“%#{search[:ship_to_last_name]}%”
end
find(:all, :conditions => conditions )
end

because I get this error:

Symbol as array index

What is the right way to pass and accept the hash into my function?
Thanks, - Dave

Hi,

Hi, I want to pass the “params” hash to a function,

def self.search(*search)

just get rid of the *. By using that, you are instructing your function
to create an array with all the arguments you are passing, which is
convenient for variable number of arguments. In the code you wrote
search[0] would be your params object. Since you don’t really need to
pass a variable number of arguments, but a single hash, just drop the *
and leave it like

def self.search(search)

that should work.

regards,

javier ramirez

Thanks for the reply, but removing the “*” didn’t work. Instead I got
this new error (listed below).

The line being referred to was "if !search[:user_type_id].is_blank? "
so I’m assuming I still didn’t pass params correctly. - Dave

NoMethodError in Super admin/subscriberController#searchresults

You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.is_blank?
RAILS_ROOT: /usr/local/apache2/htdocs/easyrx
Application Trace | Framework Trace | Full Trace

app/models/user.rb:26:in search' app/controllers/super_admin/subscriber_controller.rb:15:insearchresults’

/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/
action_controller/base.rb:1158:in send' /usr/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/ action_controller/base.rb:1158:inperform_action_without_filters’
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/
action_controller/filters.rb:697:in call_filters' /usr/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/ action_controller/filters.rb:689:inperform_action_without_benchmark’
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/
action_controller/benchmarking.rb:68:in
perform_action_without_rescue' /usr/local/lib/ruby/1.8/benchmark.rb:293:inmeasure’
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/
action_controller/benchmarking.rb:68:in
perform_action_without_rescue' /usr/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/ action_controller/rescue.rb:199:inperform_action_without_caching’
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/
action_controller/caching.rb:678:in perform_action' /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/ connection_adapters/abstract/query_cache.rb:33:incache’
/usr/local/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/
query_cache.rb:8:in cache' /usr/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/ action_controller/caching.rb:677:inperform_action’
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/
action_controller/base.rb:524:in send' /usr/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/ action_controller/base.rb:524:inprocess_without_filters’
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/
action_controller/filters.rb:685:in
process_without_session_management_support' /usr/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/ action_controller/session_management.rb:123:inprocess’
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/
action_controller/base.rb:388:in process' /usr/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/ action_controller/dispatcher.rb:171:inhandle_request’
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/
action_controller/dispatcher.rb:115:in dispatch' /usr/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/ action_controller/dispatcher.rb:126:indispatch_cgi’
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/
action_controller/dispatcher.rb:9:in dispatch' /usr/local/lib/ruby/gems/1.8/gems/rails-2.0.2/lib/webrick_server.rb: 112:inhandle_dispatch’
/usr/local/lib/ruby/gems/1.8/gems/rails-2.0.2/lib/webrick_server.rb:
78:in service' /usr/local/lib/ruby/1.8/webrick/httpserver.rb:104:inservice’
/usr/local/lib/ruby/1.8/webrick/httpserver.rb:65:in run' /usr/local/lib/ruby/1.8/webrick/server.rb:173:instart_thread’
/usr/local/lib/ruby/1.8/webrick/server.rb:162:in start' /usr/local/lib/ruby/1.8/webrick/server.rb:162:instart_thread’
/usr/local/lib/ruby/1.8/webrick/server.rb:95:in start' /usr/local/lib/ruby/1.8/webrick/server.rb:92:ineach’
/usr/local/lib/ruby/1.8/webrick/server.rb:92:in start' /usr/local/lib/ruby/1.8/webrick/server.rb:23:instart’
/usr/local/lib/ruby/1.8/webrick/server.rb:82:in start' /usr/local/lib/ruby/gems/1.8/gems/rails-2.0.2/lib/webrick_server.rb: 62:indispatch’
/usr/local/lib/ruby/gems/1.8/gems/rails-2.0.2/lib/commands/servers/
webrick.rb:66
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
gem_original_require' /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:inrequire’
/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/
active_support/dependencies.rb:496:in require' /usr/local/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/ active_support/dependencies.rb:342:innew_constants_in’
/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/
active_support/dependencies.rb:496:in require' /usr/local/lib/ruby/gems/1.8/gems/rails-2.0.2/lib/commands/server.rb: 39 /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:ingem_original_require’
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
`require’
script/server:3

app/models/user.rb:26:in search' app/controllers/super_admin/subscriber_controller.rb:15:insearchresults’
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/
action_controller/base.rb:1158:in send' /usr/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/ action_controller/base.rb:1158:inperform_action_without_filters’
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/
action_controller/filters.rb:697:in call_filters' /usr/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/ action_controller/filters.rb:689:inperform_action_without_benchmark’
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/
action_controller/benchmarking.rb:68:in
perform_action_without_rescue' /usr/local/lib/ruby/1.8/benchmark.rb:293:inmeasure’
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/
action_controller/benchmarking.rb:68:in
perform_action_without_rescue' /usr/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/ action_controller/rescue.rb:199:inperform_action_without_caching’
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/
action_controller/caching.rb:678:in perform_action' /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/ connection_adapters/abstract/query_cache.rb:33:incache’
/usr/local/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/
query_cache.rb:8:in cache' /usr/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/ action_controller/caching.rb:677:inperform_action’
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/
action_controller/base.rb:524:in send' /usr/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/ action_controller/base.rb:524:inprocess_without_filters’
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/
action_controller/filters.rb:685:in
process_without_session_management_support' /usr/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/ action_controller/session_management.rb:123:inprocess’
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/
action_controller/base.rb:388:in process' /usr/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/ action_controller/dispatcher.rb:171:inhandle_request’
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/
action_controller/dispatcher.rb:115:in dispatch' /usr/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/ action_controller/dispatcher.rb:126:indispatch_cgi’
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/
action_controller/dispatcher.rb:9:in dispatch' /usr/local/lib/ruby/gems/1.8/gems/rails-2.0.2/lib/webrick_server.rb: 112:inhandle_dispatch’
/usr/local/lib/ruby/gems/1.8/gems/rails-2.0.2/lib/webrick_server.rb:
78:in service' /usr/local/lib/ruby/1.8/webrick/httpserver.rb:104:inservice’
/usr/local/lib/ruby/1.8/webrick/httpserver.rb:65:in run' /usr/local/lib/ruby/1.8/webrick/server.rb:173:instart_thread’
/usr/local/lib/ruby/1.8/webrick/server.rb:162:in start' /usr/local/lib/ruby/1.8/webrick/server.rb:162:instart_thread’
/usr/local/lib/ruby/1.8/webrick/server.rb:95:in start' /usr/local/lib/ruby/1.8/webrick/server.rb:92:ineach’
/usr/local/lib/ruby/1.8/webrick/server.rb:92:in start' /usr/local/lib/ruby/1.8/webrick/server.rb:23:instart’
/usr/local/lib/ruby/1.8/webrick/server.rb:82:in start' /usr/local/lib/ruby/gems/1.8/gems/rails-2.0.2/lib/webrick_server.rb: 62:indispatch’
/usr/local/lib/ruby/gems/1.8/gems/rails-2.0.2/lib/commands/servers/
webrick.rb:66
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
gem_original_require' /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:inrequire’
/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/
active_support/dependencies.rb:496:in require' /usr/local/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/ active_support/dependencies.rb:342:innew_constants_in’
/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/
active_support/dependencies.rb:496:in require' /usr/local/lib/ruby/gems/1.8/gems/rails-2.0.2/lib/commands/server.rb: 39 /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:ingem_original_require’
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
`require’
script/server:3

Request
Parameters:
{“user”=>{“user_type_id”=>“1”},
“ship_to_last_name”=>"",
“commit”=>“Search”,
“ship_to_first_name”=>""}

You are most wise. The “is_blank?” was a problem … should’ve been
what you said. But also there was no :user_type_id, which you also
pointed out. I had incorrectly entered the user_type_id field name in
the search form.

Thanks so much, hope I can help you out some time, - Dave

Hi,

Thanks for the reply, but removing the “*” didn’t work. Instead I got
this new error (listed below).

actually it worked… now you are properly passing the params object

this thing you see is a new error

The line being referred to was "if !search[:user_type_id].is_blank? "
so I’m assuming I still didn’t pass params correctly. - Dave

well… there are two different things here. First is it would seem
there is no any object in your hash for the [:user_type_id] key, so you
are getting a nil here. Second, unless you defined it yourself, I have
no idea what the “is_blank?” method is. Maybe you were trying yo use the
“blank?” method? In rails you can invoke “blank?” on a nil object
without any problems. I mean

if !search[:user_type_id].blank?

regards,

javier ramirez

@Dave,

Just for fun, maybe something a little more elegant (the repetition
screams for refactoring but you get the idea)…

def search(search={})
query_string = ‘’
conditions = []
unless search[:user_type_id].blank?
query_string << “user_type_id = ?”
conditions.push search[:user_type_id]
end

[:ship_to_first_name, :ship_to_last_name].each do |like_field|
unless search[like_field].blank?
query_string << “#{like_field.to_s} LIKE ?”
conditions.push search[like_field]
end
end

return nil if conditions.empty?
find(:all, :conditions=>conditions)
end

The main thing that this does is that it takes advantage of ARec’s
ability to sanitize the query string against injection attacks.

AndyV

On Feb 10, 3:33 pm, “[email protected]