views:

46

answers:

2

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!

A: 

This is not an issue with rails, but rather a way that mysql behaves.

For example:

mysql> select first_name from users where first_name = "George                    ";
+------------+
| first_name |
+------------+
| George     |
| George     |
+------------+
2 rows in set (0.00 sec)

Here's one way to fix it:

mysql> select first_name from users where BINARY(first_name) = BINARY("George                    ");
Empty set (0.00 sec)

mysql> select first_name from users where BINARY(first_name) = BINARY("George");
+------------+
| first_name |
+------------+
| George     |
| George     |
+------------+
2 rows in set (0.00 sec)

Good luck!

Gdeglin
see comment on other answer
so1o
This example is using VARCHAR. However I see that it only applies with trailing spaces, so it doesn't fit to the op's example.
Gdeglin
ps the column is varchar
so1o
Let us know if wrapping the strings in BINARY() changes anything.
Gdeglin
+2  A: 

First turn on the general quey 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' ]
vladr
tried this.. i even tried find_by_sql, tried :conditions => {:org => 'test org'}
so1o
its the mysql gem. mysql (2.8.1)i will try updating it. but i have been using the gem for a while.. never had any problem.
so1o
ok.. will try this
so1o
yup.. you were right the information is being queried fine.. the logs are being written like that. thanks.
so1o