# Extra quotes when substittuting for ? in find\_by\_sql

**URL:** <https://www.ruby-forum.com/t/extra-quotes-when-substittuting-for-in-find-by-sql/124831>\
**Category:** Rails\
**Created:** [January 2, 2008, 11:27pm UTC](https://www.ruby-forum.com/t/extra-quotes-when-substittuting-for-in-find-by-sql/124831 "2008-01-02T23:27:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mgiorno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mgiorno](https://www.ruby-forum.com/u/mgiorno)\
**Post date:** [January 2, 2008, 11:27pm UTC](https://www.ruby-forum.com/t/extra-quotes-when-substittuting-for-in-find-by-sql/124831/1 "2008-01-02T23:27:59Z")

</div>

Hi,

I’m trying to select items created within a certain date range, but seem  
to keep getting a small syntax error - extra single quotes - whenever I  
try to use the built in range helper.

This works fine.

@temp\_top\_ten = Video.find\_by\_sql([“select \* from votes WHERE created\_at  
BETWEEN ? AND ?”, (1.month.ago).to\_s(:db), (Time.now).to\_s(:db)])

But when I combine them to use the helper link this:

@temp\_top\_ten = Video.find\_by\_sql([“select \* from votes WHERE created\_at  
?”, (1.month.ago…Time.now).to\_s(:db)])

I get this error MYSQL error becuase my querry is formatted by rails  
with extra quotes and escaped quotes: select \* from votes WHERE  
created\_at ‘BETWEEN ‘2007-12-03 17:17:07’ AND ‘2008-01-02 17:17:07’’

Is there some trick to correct this formating? Thanks so much for the  
help!

---

<div class="post-metadata">

**Author:** ![mgiorno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mgiorno](https://www.ruby-forum.com/u/mgiorno)\
**Post date:** [January 3, 2008, 2:35am UTC](https://www.ruby-forum.com/t/extra-quotes-when-substittuting-for-in-find-by-sql/124831/2 "2008-01-03T02:35:01Z")

</div>

On 1/2/08, Mario F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> @temp\_top\_ten = Video.find\_by\_sql([“select \* from votes WHERE created\_at  
> ?”, (1.month.ago…Time.now).to\_s(:db)])

Vote.find( :all, :conditions =\> [ “created\_at #{  
(5.days.ago…Time.now).to\_s(:db) }” ] )

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![mgiorno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mgiorno](https://www.ruby-forum.com/u/mgiorno)\
**Post date:** [January 3, 2008, 2:39am UTC](https://www.ruby-forum.com/t/extra-quotes-when-substittuting-for-in-find-by-sql/124831/3 "2008-01-03T02:39:34Z")

</div>

Thanks, Greg!

I only wish I could use the built in find methods. I didn’t include my  
whole query so I could keep my question easy to read, but I’ve got a  
number of other conditions and counting going on that prevent me from  
using those built in methods. My votes table is also going to be quite  
large, so I’m trying to keep as much as possible in sql for performance  
reasons.

Thanks!

---

<div class="post-metadata">

**Author:** ![mgiorno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mgiorno](https://www.ruby-forum.com/u/mgiorno)\
**Post date:** [January 3, 2008, 3:29am UTC](https://www.ruby-forum.com/t/extra-quotes-when-substittuting-for-in-find-by-sql/124831/4 "2008-01-03T03:29:21Z")

</div>

On Jan 2, 2:27 pm, Mario F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I get this error MYSQL error becuase my querry is formatted by rails  
> with extra quotes and escaped quotes: select \* from votes WHERE  
> created\_at ‘BETWEEN '2007-12-03 17:17:07' AND '2008-01-02 17:17:07'’

As far as I can see, this bug isn’t in the ActiveRecord class, but by  
the connector’s quote method.

Just in case you wanted to track the problem down yourself.

///ark
