Addy Addy - 7 months ago 8
Ruby Question

Conditions with whitespaces in activerecord find

So i am trying to do this

Order.find :all, :conditions => "org = 'test org'"


what ends up firing is

SELECT * FROM `orders` WHERE (org = 'test org')


the whitespace in the argument gets stripped. what am i missing.. im really stumped here. please help!

Answer

First turn on the general query log in MySQL and see whether the query, as received and executed by MySQL itself, is correct.

This is because Rails will squeeze all duplicate whitespace from its own query logs (when running in development mode):

  # /rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
  def log_info(sql, name, ms)
    if @logger && @logger.debug?
      name = '%s (%.1fms)' % [name || 'SQL', ms]
      @logger.debug(format_log_entry(name, sql.squeeze(' ')))
    end
  end

If the query sent to MySQL, according to the MySql query log itself, is still incorrect, try again with:

Order.find :all, :conditions => [ 'org = ?', 'test     org' ]