# Rails ActiveRecord find w/ conditions

**URL:** <https://www.ruby-forum.com/t/rails-activerecord-find-w-conditions/122140>\
**Category:** Rails\
**Created:** [December 4, 2007, 9:37pm UTC](https://www.ruby-forum.com/t/rails-activerecord-find-w-conditions/122140 "2007-12-04T21:37:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nabeelz6](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/9fc348/32.png) [@nabeelz6](https://www.ruby-forum.com/u/nabeelz6)\
**Post date:** [December 4, 2007, 9:37pm UTC](https://www.ruby-forum.com/t/rails-activerecord-find-w-conditions/122140/1 "2007-12-04T21:37:40Z")

</div>

Hey guys! First time posting so let see how this goes… got a question:

I’m trying to find a record in a table minus any leading / trailing  
whitespaces.

This code will find the record but it wont return anything if the  
value in the table’s column (named MyColumn) had any whitespaces at  
the beginning or at the end of it:  
found\_record = MyTable.find(:first,:conditions =\> [ “MyColumn = ?”,  
someValue ])

I tried this and it obviously didnt work:  
found\_record = MyTable.find(:first,:conditions =\> [ “MyColumn.strip =  
?”, someValue ])

I dont wanna modify the data in the table, just find a match.  
What can I do? Thanks!

---

<div class="post-metadata">

**Author:** ![nabeelz6](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/9fc348/32.png) [@nabeelz6](https://www.ruby-forum.com/u/nabeelz6)\
**Post date:** [December 4, 2007, 9:47pm UTC](https://www.ruby-forum.com/t/rails-activerecord-find-w-conditions/122140/2 "2007-12-04T21:47:17Z")

</div>

On 4 Dec 2007, at 20:37, Nabeel Mr. wrote:

> found\_record = MyTable.find(:first,:conditions =\> [ “MyColumn = ?”,  
> someValue ])
> 
> I tried this and it obviously didnt work:  
> found\_record = MyTable.find(:first,:conditions =\> [ “MyColumn.strip =  
> ?”, someValue ])

At this point you’ve got 2 solutions:

- at the point of saving the record, strip the whitespace. The whole  
problem goes away completely (that’s what I’d do).
- drop down to some slightly rawer sql. Assuming your database has a  
function that removes trailing and leading whitespace (in mysql it’s  
TRIM) you can do something like  
MyTable.find(:first,:conditions =\> [“TRIM(MyColumn) = ?”,someValue])

Fred

---

<div class="post-metadata">

**Author:** ![nabeelz6](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/9fc348/32.png) [@nabeelz6](https://www.ruby-forum.com/u/nabeelz6)\
**Post date:** [December 4, 2007, 9:53pm UTC](https://www.ruby-forum.com/t/rails-activerecord-find-w-conditions/122140/3 "2007-12-04T21:53:45Z")

</div>

The :conditions option gets plugged into a sql statement, so you can  
use whatever facilities your database provides for that sort of  
thing. In PostgreSQL, I’d do something like:

:conditions =\> [“trim(both ’ ’ from MyColumn) = ?”, someValue]

Peace,  
Phillip

---

<div class="post-metadata">

**Author:** ![nabeelz6](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/9fc348/32.png) [@nabeelz6](https://www.ruby-forum.com/u/nabeelz6)\
**Post date:** [December 4, 2007, 10:23pm UTC](https://www.ruby-forum.com/t/rails-activerecord-find-w-conditions/122140/4 "2007-12-04T22:23:07Z")

</div>

Thanks guys! The TRIM SQL statement worked like a charm 🙂
