binarylogic / searchlogic

Searchlogic provides object based searching, common named scopes, and other useful tools.
http://rdoc.info/projects/binarylogic/searchlogic
MIT License
1.39k stars 133 forks source link

Inconsistent result of "order" method #10

Closed ghost closed 15 years ago

ghost commented 15 years ago

Please have a look at this:

@@@ ruby

Document.search("descend_by_datetime" => true).order => nil

Document.search(:order => "descend_by_datetime").order => "descend_by_datetime"

Document.search(:order => "datetime DESC").order => "datetime DESC" @@@

IMHO the "order" method should always return the same result.

original LH ticket

This ticket has 0 attachment(s).

ghost commented 15 years ago

Inconsistent result of "order" method

You make a good point here, but for your examples to pass it would change the underlying logic behind the Search class. The point is just to store values that map to a named scope. So "decend_by_datetime" and :order => "descend_by_datetime" are 2 different named scoped. The order named scope is really just an alias that calls other named scopes. The whole point of it is to make searching with the search object easier. My suggestion is to use "order" when dealing with the search object, and if you aren’t you can do whatever you want.

What do you think?

by Ben Johnson

ghost commented 15 years ago

Inconsistent result of "order" method

I’m trying to understand, hm, but there is still something confusing if I want to get the order of an existing search object. I find out that using proxy_options gives better result:

@@@ ruby

Document.search("descend_by_datetime" => true).proxy_options[:order] => "documents.datetime DESC" # OK!

Document.search("descend_by_datetime" => true).construct_finder_sql({}) => "SELECT * FROM documents ORDER BY documents.datetime DESC" # OK! @@@

@@@ ruby

Document.search(:order => "descend_by_datetime").proxy_options[:order] => "documents.datetime DESC" # OK!

Document.search(:order => "descend_by_datetime").construct_finder_sql({}) => "SELECT * FROM documents ORDER BY documents.datetime DESC" # OK! @@@

But what is this? @@@ ruby

Document.search(:order => "datetime DESC").order => "datetime DESC" # OK!

Document.search(:order => "datetime DESC").proxy_options => {} # ???

Document.search(:order => "datetime DESC").construct_finder_sql({}) => "SELECT * FROM documents " # ??? @@@

You see that the proxy_options are blank and the last SQL statement has no ORDER BY. Maybe I’m the only one who is confused, maybe it’s because I’m going on holiday tomorrow ;-)

Best wishes, Georg

by Georg Ledermann

ghost commented 15 years ago

Inconsistent result of "order" method

That’s because the :order condition MUST be a named scope, you can’t pass raw SQL.

by Ben Johnson

ghost commented 15 years ago

Inconsistent result of "order" method

Also, this is to protect from SQL injections

by Ben Johnson

ghost commented 15 years ago

Inconsistent result of "order" method

Ok, totally agree. To avoid irritations, what about raising an exception (like "UnknownOrderError") if someone gives raw SQL (or unknown order scope) to the :order key? I think this would be a good thing, because there is already a Searchlogic::Search::UnknownConditionError raised for wrong :condition value.

by Georg Ledermann