Search code examples
rubysqlitecase-insensitive

Ruby Case Comparison


Rookie here

I am trying to get ignore cases when comparing two twitter handles. One is in the users database which is being retrieved into the valid_handles_results array. I want to compare the Twitter handle from a separate database where twitter handles are stored at record[1] column.

I am getting the following error :

undefined method `casecmp' for ["theFancyPies"]:Array

Any suggestions?

all_orders_in_a_order_results.each do |record|

# check whether the tweet is unique or not, if yes then check whether the user has an account or not
# # the following query checks that the new entry being put in the all_orders_ordered database is unique or not
count_orders = @all_ordersDB.get_first_value('SELECT COUNT(*) FROM all_orders_ordered WHERE tweet_id = ? AND time_of_tweet = ? ' ,[record[5], record[4]])
unique_ordered = (count_orders ==0 )


# Query for selecting twitter_handles for all the users that are registered
valid_handles = %{SELECT twitter_handle FROM users}
valid_handles_results = @userDb.execute valid_handles

user_status = 0

valid_handles_results.each do |valid_handle|

      if valid_handle.casecmp(record[1]) ==0 

          user_status = 1
      else
          user_status = 0

      end

end

Solution

  • When executing your valid_handles query the result of this is an array of arrays. Even though you're only selecting a single column (SELECT twitter_handle FROM...) the entries in the valid_handles_results array are arrays containing a single element.

    So to fix the error replace valid_handle.casecmp(record[1]) with valid_handle[0].casecmp(record[1])

    While this may seem a little confusing at first, the reason behind this is that it means @userDb.execute can return a consistent structure regardless of the number of columns being selected in the query.

    To help with debugging similar errors in the future, it's really helpful to get to understand "undefined method" errors. undefined method `casecmp' for ["theFancyPies"]:Array is telling you that you tried to call casecmp but the object you tried to call it on does not have a method with that name. The :Array is telling you the class of the object you called the method on. So in this instance you had an Array when you were expecting a String.