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, 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
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
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
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
[: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.