# Rails find :conditions seems to remove extra blanks? (e.g. where description = 'x x x x x') is this

**URL:** <https://www.ruby-forum.com/t/rails-find-conditions-seems-to-remove-extra-blanks-e-g-where-description-x-x-x-x-x-is-this/161525>\
**Category:** Rails\
**Created:** [February 27, 2009, 4:17am UTC](https://www.ruby-forum.com/t/rails-find-conditions-seems-to-remove-extra-blanks-e-g-where-description-x-x-x-x-x-is-this/161525 "2009-02-27T04:17:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [February 27, 2009, 4:17am UTC](https://www.ruby-forum.com/t/rails-find-conditions-seems-to-remove-extra-blanks-e-g-where-description-x-x-x-x-x-is-this/161525/1 "2009-02-27T04:17:26Z")

</div>

Hi,  
Is this a Rails bug?

NORMAL SQL  
?\> AccountItem.find\_by\_sql( “select \* from account\_items where  
description =  
‘FISHERS IGA HAMILTON HAMILTON AU 000318’”)  
=\> [#\<AccountItem id: 1100, account\_id: 5, recurring\_id: nil, date:  
“2008-12-30”, amount: #BigDecimal:24fc840,‘-0.4E3’,4(12), balance:  
#BigDecimal:24fc69c,‘0.745538E4’,8(12), description: “FISHERS IGA  
HAMILTON  
HAMILTON AU 000318”, notes: nil, projection: false, created\_at:  
“2009-02-08 22:29:25”, updated\_at: “2009-02-08 22:29:25”\>]

> >

WITH FINDER - DOES FIND THE RECORD  
?\> AccountItem.find(:all, :conditions =\> [“description = ?”, ‘FISHERS  
IGA  
HAMILTON HAMILTON AU 000318’] )  
=\> []  
EXTRACT FROM SERVER LOG - THE CREATED SQL SEEMS TO BE MISSING THE EXTRA  
SPACES??  
[4;36;1mAccountItem Load (3.0ms) [0m [0;1mSELECT \* FROM  
`account_items` WHERE (description = ‘FISHERS IGA HAMILTON HAMILTON AU  
000318’) [0m

That is find method seems to be mis-translating the SQL and removing  
spaces.

–  
Greg  
[http://blog.gregnet.org/](http://blog.gregnet.org/)

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [February 27, 2009, 9:53pm UTC](https://www.ruby-forum.com/t/rails-find-conditions-seems-to-remove-extra-blanks-e-g-where-description-x-x-x-x-x-is-this/161525/2 "2009-02-27T21:53:39Z")

</div>

The log entries get passed through .squeeze(’ '), I’m guessing to make  
them look nicer on  
the log. (activerecord/lib/active\_record/connection\_adapters/  
abstract\_adapter.rb, line 189)

Otherwise, I can’t see anywhere in the code that’s dropping the  
spaces. Google’s formatting  
engine chews up spaces pretty bad, but are you _sure_ you’ve got the  
same number in  
that second query?

On an unrelated note, are you really sure you want a fixed-width space-  
delimited field in the DB?  
Wouldn’t it be easier to use multiple fields?

–Matt J.

On Feb 26, 10:16 pm, Greg H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [February 28, 2009, 12:51am UTC](https://www.ruby-forum.com/t/rails-find-conditions-seems-to-remove-extra-blanks-e-g-where-description-x-x-x-x-x-is-this/161525/3 "2009-02-28T00:51:20Z")

</div>

2009/2/28 Matt J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> Google’s formatting  
> engine chews up spaces pretty bad, but are you _sure_ you’ve got the  
> same number in  
> that second query?

It should be as I used the save copy/paste of the string. I take your  
point  
re the standard output and what could happen there, however the bottom  
line  
seems to be when I use exactly the same string in the “find” method it  
doesn’t find it, whereas if I use it in the “find\_by\_sql” method it does  
find the row.

I wonder how I could confirm the exact SQL that is getting issued to the  
DB  
without having to worry about any changes to it whilst it is being  
displayed. Perhaps I’ll try to switch on logging in mysql and monitor  
at  
that point?

> On an unrelated note, are you really sure you want a fixed-width space-  
> delimited field in the DB?  
> Wouldn’t it be easier to use multiple fields?

Not exactly sure what you meant here. The description field is just  
from  
CSV file for bank transaction downloads. I thought I would have a table  
that just exactly maps the CSV files into it (then uniqueness is based  
on  
Date-Amount-Description)

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [March 1, 2009, 1:13am UTC](https://www.ruby-forum.com/t/rails-find-conditions-seems-to-remove-extra-blanks-e-g-where-description-x-x-x-x-x-is-this/161525/4 "2009-03-01T01:13:29Z")

</div>

On Feb 27, 6:50 pm, Greg H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> doesn’t find it, whereas if I use it in the “find\_by\_sql” method it does  
> find the row.
> 
> I wonder how I could confirm the exact SQL that is getting issued to the DB  
> without having to worry about any changes to it whilst it is being  
> displayed. Perhaps I’ll try to switch on logging in mysql and monitor at  
> that point?

That would be a good idea. You might also want to try some queries  
like:

AccountItem.find(:all, :conditions =\> [“description LIKE ?”, ‘FISHERS  
IGA%’] )

with progressively longer substrings to see if any of those work.

> > On an unrelated note, are you really sure you want a fixed-width space-  
> > delimited field in the DB?  
> > Wouldn’t it be easier to use multiple fields?
> 
> Not exactly sure what you meant here. The description field is just from  
> CSV file for bank transaction downloads. I thought I would have a table  
> that just exactly maps the CSV files into it (then uniqueness is based on  
> Date-Amount-Description)

My thought was that this “description” field looks like more than one  
field glued together, probably screen-scraped from a bank terminal  
interface somewhere. The multiple spaces imply that it’s probably  
fixed-width, so why not break the field apart according to those fixed-  
width fields?  
(Without any knowledge of the data source, it looks like [Name, City,  
(Country?), (Account number?)])

–Matt J.
