Right way to access an array passed as an argument?

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

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

···

On Feb 9, 2008 7:09 PM, laredotornado <laredotornado@zipmail.com> wrote:

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