# ActiveRecord and SQL Regular Expressions

**URL:** <https://www.ruby-forum.com/t/activerecord-and-sql-regular-expressions/131045>\
**Category:** Rails\
**Created:** [March 5, 2008, 3:08pm UTC](https://www.ruby-forum.com/t/activerecord-and-sql-regular-expressions/131045 "2008-03-05T15:08:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![gers32](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ed8c4c/32.png) [@gers32](https://www.ruby-forum.com/u/gers32)\
**Post date:** [March 5, 2008, 3:08pm UTC](https://www.ruby-forum.com/t/activerecord-and-sql-regular-expressions/131045/1 "2008-03-05T15:08:24Z")

</div>

Hi,

I have the following code:

```
@my_things = MyThing.find(
  :all,
  :select => "ID, CODE",
  :conditions => conditions_list)

```

conditions\_list is an array, with the following elements :

[0] code like ? and comp\_cas\_no like ? and PRODUCT like ‘%R%’ and  
(PRODUCT like ? or PRODUCT like ?)  
[1] %74%  
[2] %98%  
[3] %22%  
[4] %25%

Now, I’d like to replace “PRODUCT like ?” in conditions\_list[0] with  
an Oracle Regular Expression, like this :

“REGEXP\_LIKE( PRODUCT, ‘R[[:digit| |/]\*?’)”

where “?” would be replaced with one of the other values in  
conditions\_list, just like with the query above. Unfortunately, “?”  
will be taken literally here…

I guess one option would be to use MyThing.find\_by\_sql() and recreate  
the SQL query in its entirety, but I’m hoping someone can help me keep  
MyThing.find().

Thanks,

Chris.

---

<div class="post-metadata">

**Author:** ![gers32](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ed8c4c/32.png) [@gers32](https://www.ruby-forum.com/u/gers32)\
**Post date:** [March 7, 2008, 10:58am UTC](https://www.ruby-forum.com/t/activerecord-and-sql-regular-expressions/131045/2 "2008-03-07T10:58:21Z")

</div>

Thanks to Baptiste on Railsfrance, the solution is as follows :

The query includes: REGEXP\_LIKE( PRODUCT, ?)  
The passed-in argument looks like this: “R([:digit]| |/)\*(26|36)” (26  
and 36 are examples)
