Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

AR::Base#all(:order => '') => invalid SQL

#1765

class Venue < ActiveRecord::Base
end

Venue.find(:all, :order => '')
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 '' at line 1: SELECT * FROM `venues`      ORDER BY 
        from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.1.2/lib/active_record/connection_adapters/
abstract_adapter.rb:147:in `log'

:conditions and :joins appear to work, at least in isolation.

Reported by Student · January 15th, 2009 @ 07:49 PM

State: wontfix
Milestone: 2.x
Assigned to: Pratik Pratik
Importance: none

Activity

  1. Yaroslav Markin
    Yaroslav Markin
    • Tag changed from 2.1.2, activerecord, mysql to 2.1.2, activerecord, mysql, patch, tiny
    • Assigned user set to Pratik

    Patch attached

    January 17th, 2009 @ 08:40 AM

  2. Pratik
    Pratik
    • State changed from new to wontfix

    You shouldn't supply empty string :) It's not really designed to work with conditions and :joins, but it just does. It could easily break.

    January 18th, 2009 @ 03:46 AM

  3. Student
    Student

    Is this my payment for minimizing my example? :/

    I found this using generated code. With this functionality, I can do the following:

    @@@ruby cond_string = big computation order_string = bigger computation join_string = biggest computation

    Venue.all(:joins => join_string, :conditions => cond_string, :order => order_string)

    
    
    Without, I'm stuck with:
    
    @@@ruby
    cond_string = big computation
    order_string = bigger computation
    join_string = biggest computation
    
    options = {}
    options[:conditions] = cond_string unless cond_string.empty?
    options[:order] = order_string unless order_string.empty?
    options[:joins] = join_string unless join_string.empty?
    
    Venue.all(options)
    

    Unless I "really shouldn't" pass in an empty options string, either.

    January 18th, 2009 @ 03:33 PM

  4. Student
    Student

    That didn't format as advertised, and I don't seem to be able to edit my comment.

    Is this my payment for minimizing my example? :/

    I found this using generated code. With this functionality, I can do the following:

    cond_string = big computation order_string = bigger computation join_string = biggest computation

    Venue.all(:joins => join_string, :conditions => cond_string, :order => order_string)

    Without, I'm stuck with:

    cond_string = big computation order_string = bigger computation join_string = biggest computation

    options = {} options[:conditions] = cond_string unless cond_string.empty? options[:order] = order_string unless order_string.empty? options[:joins] = join_string unless join_string.empty?

    Venue.all(options)

    Unless I "really shouldn't" pass in an empty options string, either.

    January 18th, 2009 @ 03:36 PM

  5. Student
    Student

    Ick. Spaces matter.

    Is this my payment for minimizing my example? :/

    
    cond_string = big computation 
    order_string = bigger computation 
    join_string = biggest computation
    
    Venue.all(:joins => join_string, :conditions => cond_string, :order => order_string)
    

    Without, I'm stuck with:

    
    cond_string = big computation 
    order_string = bigger computation 
    join_string = biggest computation
    
    options = {} options[:conditions] = cond_string unless cond_string.empty? 
    options[:order] = order_string unless order_string.empty? 
    options[:joins] = join_string unless join_string.empty?
    
    Venue.all(options)
    

    Unless I "really shouldn't" pass in an empty options string, either.

    January 18th, 2009 @ 03:41 PM

  6. Pratik
    Pratik
    
    order_string = bigger_computation
    order_string = nil if order_string.blank? 
    cond_string = big computation 
    join_string = biggest computation
    
    Venue.all(:joins => join_string, :conditions => cond_string, :order => order_string)
    

    January 18th, 2009 @ 06:06 PM

  7. Student
    Student

    There is currently no documentation on what happens if an option is passed which is nil or empty. I discovered this behavior (:joins, and :conditions work with blank parameters, :order does not) when using generated fragments. In my case, these generated fragments were themselves compounds, built with a final Array#join, thus always strings.

    I would like a clean, smooth, supported interface. Your solution would appear to be none of these.

    January 18th, 2009 @ 08:47 PM

  8. Yaroslav Markin
    Yaroslav Markin

    @lifo while this ticket is far from important and I fully agree that @nil@-ifying parameter is just what Student needs, I think there is still sense in applying: right now there is a code snippet that checks for @nil@, we can just change it to @.present?@, that solves the problem just right.

    January 18th, 2009 @ 08:53 PM

  9. Xavier Noria
    Xavier Noria

    I am -1 here. The API shouldn't be designed to eat everything. In particular I don't really see the documentation stating that you can pass blank?s to :order (if it was really supported it should be documented).

    In my opinion :order has either a valid value or nothing. An empty string is an invalid value.

    Another possibility would be to clean the hash with a custom #compact method for hashes that removes entries with blank? values.

    January 18th, 2009 @ 09:12 PM

  10. Student
    Student

    I agree that this is not an error as in "the documentation says X will happen and it does not". But I believe that the observed behavior is not optimal, in that the caller is responsible (in some cases) to normalize data when there is only one reasonable way to interpret the data.

    Certainly, the caller can, as I currently must, ensure that no option is set with blank parameters. But if the purpose of a framework is to reduce the work of the using programmer, this would seem to be a strange way to proceed.

    January 19th, 2009 @ 03:33 AM