Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

find with conditions bug when qouting '?' #31

Closed
apsoto opened this issue May 20, 2011 · 1 comment
Closed

find with conditions bug when qouting '?' #31

apsoto opened this issue May 20, 2011 · 1 comment

Comments

@apsoto
Copy link

apsoto commented May 20, 2011

There is a bug with find when specifying a condition when I put the quotes around the ?.

model = Model.find(:first, :conditions => ["slug = '?'", slug])

returns nil even though the record exists, because if I then try to create it fails with a uniqueness constraint.

Model.create({:slug => slug})

I notice the following in the log output when doing the find (notice the extra space in the condition)

I/APP     (  281): I 05/19/2011 20:02:26:219 0020af00                  APP| condition_str : slug = 'arts '
I/APP     (  281): I 05/19/2011 20:02:26:305 0020af00                  APP| exception when running query: could not execute statement: 19; Message: column slug is not unique
I/APP     (  281): I 05/19/2011 20:02:26:316 0020af00                  APP| create Exception: #<ArgumentError: could not execute statement: 19; Message: column slug is not unique>
I/APP     (  281): I 05/19/2011 20:02:26:319 0020af00                  APP| App error: could not execute statement: 19; Message: column slug is not unique
I/APP     (  281):  lib/rhom/rhom_db_adapter.rb:109:in `execute'
I/APP     (  281): lib/rhom/rhom_db_adapter.rb:109:in `_execute_sql'
I/APP     (  281): lib/rhom/rhom_db_adapter.rb:98:in `execute_sql'
I/APP     (  281): lib/rhom/rhom_db_adapter.rb:246:in `insert_into_table'
I/APP     (  281): lib/rhom/rhom_object_factory.rb:1458:in `create'
I/APP     (  281): lib/rhom/rhom_object_factory.rb:1345:in `create'

However, if I move the qoutes around like such:

model = Model.find(:first, :conditions => ["slug = ?", "'#{slug}'"])

And the condition string log output (notice NO extra space in the condition):

I/APP     (  279): I 05/20/2011 11:21:36:325 002d12e0                  APP| condition_str : slug = 'arts'

The record is found and therefore I can do an update instead of a create.

@genywind
Copy link
Contributor

Use this syntax:
:conditions => {'slug'=>slug}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants