# Bad sql after :conditions =\>

**URL:** <https://www.ruby-forum.com/t/bad-sql-after-conditions/157454>\
**Category:** Rails\
**Created:** [January 11, 2009, 3:03pm UTC](https://www.ruby-forum.com/t/bad-sql-after-conditions/157454 "2009-01-11T15:03:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gotar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/7ab992/32.png) [@gotar](https://www.ruby-forum.com/u/gotar)\
**Post date:** [January 11, 2009, 3:03pm UTC](https://www.ruby-forum.com/t/bad-sql-after-conditions/157454/1 "2009-01-11T15:03:07Z")

</div>

I’ve something like that:

in layout :  
links looks that :

\<%= link\_to month.strftime(’%B, %Y’),  
“/?y=#{month.strftime(”%Y")}&m=#{month.strftime("%m")}" %\>

in controler I’ve :

if params[:y] && params[:m]  
@posts = Post.find(:all,:order =\> ‘created\_at’ ,:conditions =\>  
[‘extract(year from created\_at) LIKE ? AND extract(month from  
created\_at) LIKE ?’, params[:y], params[:m]] )  
else  
@posts = Post.find(:all)  
end

but in console I’ve got that sql code:  
SELECT \* FROM “posts” WHERE (extract(year from created\_at) LIKE E’2009’  
AND extract(month from created\_at) LIKE E’01’) ORDER BY created\_at

looks like this E near that LIKE broke everything

some idea how to repair that

---

<div class="post-metadata">

**Author:** ![gotar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/7ab992/32.png) [@gotar](https://www.ruby-forum.com/u/gotar)\
**Post date:** [January 11, 2009, 4:38pm UTC](https://www.ruby-forum.com/t/bad-sql-after-conditions/157454/2 "2009-01-11T16:38:17Z")

</div>

Oskar Szrajer wrote:

> I’ve something like that:
> 
> in layout :  
> links looks that :
> 
> \<%= link\_to month.strftime(’%B, %Y’),  
> “/?y=#{month.strftime(”%Y")}&m=#{month.strftime("%m")}" %\>

Don’t write a raw URI path and query, and don’t use string surgery to  
create  
one. Add those items as the arguments to url\_for():

\<%= link\_to month.strftime(’%B, %Y’),  
:y =\> month.year, :m =\> month.month %\>

(BTW naming a date ‘month’ is kind’a tacky…)

That would escape your URI correctly, but this can’t be your problem  
because  
either way these are just numbers, not punctuation that needs escapes.

> but in console I’ve got that sql code:  
> SELECT \* FROM “posts” WHERE (extract(year from created\_at) LIKE E’2009’  
> AND extract(month from created\_at) LIKE E’01’) ORDER BY created\_at
> 
> looks like this E near that LIKE broke everything

I can’t see where the E comes from, but why the LIKE? Your year and  
month have  
no % in them for LIKE to wildcard.

Can your “functional” test call this action correctly?

–  
Phlip

---

<div class="post-metadata">

**Author:** ![gotar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/7ab992/32.png) [@gotar](https://www.ruby-forum.com/u/gotar)\
**Post date:** [January 11, 2009, 5:05pm UTC](https://www.ruby-forum.com/t/bad-sql-after-conditions/157454/3 "2009-01-11T17:05:58Z")

</div>

I think there will not a problem in link\_to but I’ve try to use a  
url\_for

LIKE in condition is not a problem too I’ve check with = and still  
getting something similary:

@posts = Post.find(:all,:order =\> ‘created\_at’ ,:conditions =\>  
[‘extract(year from created\_at) = ? AND extract(month from created\_at) =  
?’, params[:y], params[:m]] )

gives :

SELECT \* FROM “posts” WHERE (extract(year from created\_at) = E’2009’ AND  
extract(month from created\_at) = E’01’) ORDER BY created\_at

still this E exist in sql code

---

<div class="post-metadata">

**Author:** ![gotar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/7ab992/32.png) [@gotar](https://www.ruby-forum.com/u/gotar)\
**Post date:** [January 11, 2009, 5:15pm UTC](https://www.ruby-forum.com/t/bad-sql-after-conditions/157454/4 "2009-01-11T17:15:25Z")

</div>

hmm after changing LIKE to =

in sql code is still "E "

but it’s start works, return proper page ?? now it’s big question for me  
what that E is and whay it’s work now?

---

<div class="post-metadata">

**Author:** ![gotar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/7ab992/32.png) [@gotar](https://www.ruby-forum.com/u/gotar)\
**Post date:** [January 11, 2009, 5:21pm UTC](https://www.ruby-forum.com/t/bad-sql-after-conditions/157454/5 "2009-01-11T17:21:20Z")

</div>

ok I’ve got answer somewhere else :

> **[4.1. Lexical Structure](https://www.postgresql.org/docs/8.3/sql-syntax-lexical.html)**
>
> 4.1. Lexical Structure 4.1.1. Identifiers and Key Words 4.1.2. Constants 4.1.3. Operators 4.1.4. Special Characters 4.1.5. Comments 4.1.6. Operator Precedence SQL …

this E is a part of postgres “”

---

<div class="post-metadata">

**Author:** ![gotar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/7ab992/32.png) [@gotar](https://www.ruby-forum.com/u/gotar)\
**Post date:** [January 11, 2009, 5:22pm UTC](https://www.ruby-forum.com/t/bad-sql-after-conditions/157454/6 "2009-01-11T17:22:24Z")

</div>

Oskar Szrajer wrote:

> hmm after changing LIKE to =
> 
> in sql code is still "E "
> 
> but it’s start works, return proper page ?? now it’s big question for me  
> what that E is and whay it’s work now?

What is in the params = {}, in the output of your script/console when  
you run that?

---

<div class="post-metadata">

**Author:** ![gotar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/7ab992/32.png) [@gotar](https://www.ruby-forum.com/u/gotar)\
**Post date:** [January 11, 2009, 5:25pm UTC](https://www.ruby-forum.com/t/bad-sql-after-conditions/157454/7 "2009-01-11T17:25:19Z")

</div>

Processing PostsController#index (for 127.0.0.1 at 2009-01-11 17:24:48)  
[GET]  
Parameters: {“m”=\>“01”, “y”=\>“2009”}  
SQL (0.1ms) SET client\_min\_messages TO ‘panic’  
SQL (0.1ms) SET client\_min\_messages TO ‘notice’  
Post Load (4.5ms) SELECT \* FROM “posts” WHERE (extract(year from  
created\_at) = E’2009’ AND extract(month from created\_at) = E’01’) ORDER  
BY created\_at  
Rendering template within layouts/application  
Rendering posts/index  
SQL (1.1ms) SELECT count(_) AS count\_all FROM “comments” WHERE  
(“comments”.post\_id = 2)  
CACHE (0.0ms) SELECT count(_) AS count\_all FROM “comments” WHERE  
(“comments”.post\_id = 2)  
Rendered posts/\_post (23.8ms)  
SQL (0.6ms) SELECT count(_) AS count\_all FROM “comments” WHERE  
(“comments”.post\_id = 1)  
CACHE (0.0ms) SELECT count(_) AS count\_all FROM “comments” WHERE  
(“comments”.post\_id = 1)  
Rendered posts/\_post (4.5ms)  
Post Load (13.5ms) SELECT \* FROM “posts” ORDER BY created\_at  
Rendered arrays/\_array (0.7ms)  
Completed in 72ms (View: 43, DB: 20) | 200 OK  
[[http://127.0.0.1/?y=2009&m=01](http://127.0.0.1/?y=2009&m=01)]
