#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?


>> 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}%"
    {:conditions => [conds.join(" AND "), vals]}

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

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

Comments and changes to this ticket

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


Referenced by