#106 ✓resolved
Falk Pauser

order by a custom named_scope

Reported by Falk Pauser | July 24th, 2009 @ 09:20 AM

Given the model, controller and view below (nothing special) I wonder why the given named_scopes are ignored. Is this a rails bug regarding named_scope-chaining, or could searchlogic be improved?

# EXAMPLE

>> Person.search(:suche => "bla bla", :order => "descend_by_custom").first
   # tailing the development.log shows a sql-query missing the order-statement

>> Person.descend_by_custom.search(:suche => "bla bla").first
   # works as expected
# model
class Person < ActiveRecord::Base
  [..]
  named_scope :ascend_by_custom, :order => "CONCAT_WS('',lastname,company,firstname) ASC"
  named_scope :descend_by_custom, :order => "CONCAT_WS('',lastname,company,firstname) DESC"
  named_scope :suche => lambda {|querystr|
    conds, vals = [], {}
    querystr.split(/\s+/).each_with_index do |q, index|
      %w(firstname lastname company).each {|attr| conds[index] << ".#{attr} like :q#{index}" }
      conds[index] = "(" + conds[index].join(" OR ") + ")"
      vals[:"q#{index}"] = "%#{q}%"
    end
    {:conditions => [conds.join(" AND "), vals]}
  }
  [..]
end

# controller
[..] @search = Person.search(params[:search]) [..]

# view
[..] <%= order(@search, :by => "custom", :as => "Person") %> [..]

Comments and changes to this ticket

  • Falk Pauser

    Falk Pauser July 27th, 2009 @ 09:42 AM

    • Tag set to activerecord, named_scope, order

    Let me illustrate the problem better (rails-2.3.3):

    given that model:

    class User < ActiveRecord::Base
      default_scope :order => "users.vorname ASC, users.nachname ASC"
      named_scope :default_order, :order => "users.id ASC, users.vorname ASC"
      named_scope :ascend_by_vorname, :order => "users.vorname ASC"
      named_scope :ascend_by_nachname, :order => "users.nachname ASC"
      named_scope :suche, lambda {|query|

    {:conditions =&gt; [&quot;users.vorname like ? OR users.nachname like ?&quot;, &quot;%#{query}%&quot;, &quot;%#{query}%&quot;]}
    
    
    
    
    } end

    script/console-session

    User.search(:suche => "a").all

    output: SELECT * FROM "users" WHERE (users.vorname like '%a%' OR users.nachname like '%a%') ORDER BY users.vorname ASC, users.nachname ASC

    
    
    

    => [ok] default_scope was called

    
    
    
    User.search(:suche => "a", :order => "ascend_by_vorname").all

    output: SELECT * FROM "users" WHERE (users.vorname like '%a%' OR users.nachname like '%a%') ORDER BY users.vorname ASC, users.nachname ASC

    
    
    

    => [failure] default_scope was called instead of ascend_by_vorname // given order

    
    
    

    => [fix] commenting-out default_scope helps

    
    
    
    User.ascend_by_vorname.search(:suche => "a").all

    output: SELECT * FROM "users" WHERE (users.vorname like '%a%' OR users.nachname like '%a%') ORDER BY users.vorname ASC, users.nachname ASC

    
    
    

    => [ok] ascend_by_vorname was called

    
    
    
    User.suche("a").ascend_by_vorname.all

    output: SELECT * FROM "users" WHERE (users.vorname like '%a%' OR users.nachname like '%a%') ORDER BY users.vorname ASC

    
    
    

    => [ok] ascend_by_vorname was called

    
    
    
    User.suche("a", :order => "default_order").all

    output: SELECT * FROM "users" WHERE (users.vorname like '%a%' OR users.nachname like '%a%')

    
    
    

    => [failure] no scope gets called (although named_scope was disabled)

    
    
    

    If I add a column called "default_order" to my users-table the default_order-scope will work. To sum it up it looks like this:

    Searchlogic does not like custom-named-scopes when it comes to ordering.
    When set - searchlogic does not override a default_scope, so i cannot use default_scopes
    together with searchlogic...

  • Falk Pauser

    Falk Pauser July 27th, 2009 @ 11:26 AM

    Just created a spec illustrating better what i mentioned before

  • Falk Pauser

    Falk Pauser July 27th, 2009 @ 03:46 PM

    • Tag changed from activerecord, named_scope, order to activerecord, named_scope, order, patch
  • Falk Pauser

    Falk Pauser July 28th, 2009 @ 07:08 PM

    • Tag changed from activerecord, named_scope, order, patch to activerecord, named_scope, order

    failing test:

      it "should have order by custom scope" do
        User.column_names.should_not include("custom")
        %w(bjohnson thunt fisons).each { |username| User.create(:username => username) }
        User.named_scope(:ascend_by_custom, :order => "username ASC, name DESC")
        User.order("ascend_by_custom").proxy_options.should == User.ascend_by_custom.proxy_options
      end
    
  • Falk Pauser
  • Ben Johnson

    Ben Johnson July 29th, 2009 @ 12:35 PM

    • State changed from “new” to “resolved”

    Sweet deal, this has been applied. Thanks!

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

Provides common named scopes and object based searching.

People watching this ticket

Attachments

Referenced by

Pages