Right way to access an array passed as an argument?

L

laredotornado

Hi,

In my user model class, I have:

class User < ActiveRecord::Base
....
def self.search(search)
conditions = []
if !
search[:user_type_id].is_empty? # line
26
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

I am passing my "params" array to this function. The array params
contains a hidden field, "user_type_id," that is always defined. But
when I execute the above, I get the error


You have a nil object when you didn't expect it!
The error occurred while evaluating nil.is_empty?
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:in
`searchresults'


so I'm thinking "search[:user_type_id]" is not the right way to access
the parameter value. If I try ":search[:user_type_id]", I also get
the error.

Thanks for any help, - Dave
 
P

PullMonkey

[Note: parts of this message were removed to make it a legal post.]

You probably want is_blank? since search[:user_type_id] is a value.
is_blank? returns true if nil or an empty string
is_empty? returns true if the array has no elements
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,689
Members
48,796
Latest member
Greg L.

Latest Threads

Top