Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #10764 (closed defect: fixed)

Opened 11 months ago

Last modified 8 months ago

[PATCH] Invalid query with blank :conditions in a nested with_scope find

Reported by: mcmire Assigned to: core
Priority: normal Milestone: 2.x
Component: ActiveRecord Version: edge
Severity: normal Keywords: verified
Cc:

Description

I did a search and found ticket #7599 which made it possible to write e.g.

Person.find(:first, :conditions => "")

without running into a SQL error. It seems, however, that this error crops back up with a nested with_scope:

Person.with_scope :find => { :conditions => "" } do
   Person.with_scope :find => { :conditions => "" } do
      Person.find(:first)
   end
end

The exact error I got was:

ActiveRecord::StatementInvalid: Mysql::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') AND (  ))  LIMIT 1' at line 1: SELECT * FROM `products`   WHERE ((  ) AND (  ))  LIMIT 1

I did some snooping about in the Rails source and I think the culprit may be, unsurprisingly, ActiveRecord::Base.with_scope itself, specifically this passage:

if key == :conditions && merge
  hash[method][key] = [params[key], hash[method][key]].collect{ |sql| "( %s )" % sanitize_sql(sql) }.join(" AND ")
elsif...

I believe that making sure that sql is not blank before assigning to hash[method][key] will fix this bug, or it might possibly be as simple as writing .compact before the join, or perhaps a combination of both.

I'll do some more testing later today, but this is just a heads up.

Attachments

blank_conditions_in_nested_scopes.patch (3.3 kB) - added by cavalle on 03/01/08 23:07:40.

Change History

01/10/08 23:26:44 changed by mcmire

For the record, something like this also fails

Product.with_scope :find => { :conditions => "deactivated = 1" } do
   Product.with_scope :find => { :conditions => "" } do
      Product.find(:first)
   end
end

as does this

Product.with_scope :find => { :conditions => "" } do
   Product.with_scope :find => { :conditions => "deactivated = 1" } do
      Product.find(:first)
   end
end

03/01/08 23:07:40 changed by cavalle

  • attachment blank_conditions_in_nested_scopes.patch added.

03/01/08 23:09:52 changed by cavalle

  • summary changed from Invalid query with blank :conditions in a nested with_scope find to [PATCH] Invalid query with blank :conditions in a nested with_scope find.

Tests (based on #7599) and patch attached

03/01/08 23:44:46 changed by mcmire

Whoops, I'd forgotten about this. Nice solution.

03/06/08 12:44:50 changed by juanjo.bazan

+1 Works for me. Solved elegantly

03/06/08 19:23:02 changed by danger

+1 Delightful.

03/22/08 18:50:02 changed by h-lame

+1

03/22/08 18:52:02 changed by cavalle

  • keywords set to verified.

03/23/08 05:00:28 changed by bitsweat

  • status changed from new to closed.
  • resolution set to fixed.

(In [9082]) Fix merging blank conditions. Closes #10764 [mcmire, cavalle]