0

我写了两个简单的关系助手:

  • with_preferred_sort_order返回一个关系
  • all_on_page返回[pages_count, record_array]

with_preferred_sort_order 奇怪地对记录进行排序:

1.9.3p194 :040 > Article.with_preferred_sort_order.
all_on_page(1).last.map{|a| puts a.title[0..20]};nil
  (0.5ms)  SELECT COUNT(*) FROM "articles" 
  Article Load (157.9ms)  SELECT "articles".* FROM "articles" 
ORDER BY title Desc LIMIT 50 OFFSET 0

“The Bar for Success 
method_missing in Jav
jQuery on Rails: A Fr
jQuery in Action (w00
jQuery in Action (w00
jQuery Selector Refca
jQuery Selector Refca
jQuery Selector Refca
jQuery 1.4 and Malfor
jQuery 1.4 and Malfor
What’s Wrong with “HT
What’s Up With All Th
What’s Up With All Th
What’s Up With All Th
What’s Up With All Th
What’s New in Bundler
Vibrant Ink Theme for
Using the New Gem Bun
Using the New Gem Bun
Using SproutCore 2.0 
Using SproutCore 2.0 
Using SproutCore 2.0 
Using >= Considered H
Understanding “Protot
Understanding JavaScr
Tokaido: My Hopes and
Tokaido Status Update
Today’s Dispatch: Wea
Threads (in Ruby): En
Threads (in Ruby): En
Threads (in Ruby): En
The Rails 3 Router: R
The Rails 3 Router: R
The Irony of the iPad
Textmate gem
Textmate Search in a 
Stop Watching Sophie’
Spinning up a new Rai
Spinning up a new Rai
Some of the Problems 
Simplifying Rails Blo
Search within a folde
Ruby 2.0 Refinements 
Ruby 2.0 Refinements 
Ruby 2.0 Refinements 
RailsConf Talk Recap
RailsConf Slides
RailsConf Slides
RailsConf Slides
RailsConf Europe Slid
 => nil 
1.9.3p194 :041 > 

这里是源

  def self.included recipient
    with recipient do
      scope :with_preferred_sort_order, 
        lambda { 
          #sort_by  = recipient.preferences.sort_by; 
          #asc_desc = recipient.preferences.asc_desc; 
          #recipient.order("%s %s"%[sort_by, asc_desc]) 
          sort_by  = preferences.sort_by; 
          asc_desc = preferences.asc_desc; 
          order("%s %s"%[sort_by, asc_desc]) 
        }
    end
  end

还有一个

module ActiveRecord
  class Relation
    # Returns an array [ pages_range, items_array]
    #   pages_range is a range of pages, which starts from 1, e.g. 1..19
    #   items_array is an array which contains items of page_number page
    #
    def all_on_page page_number=1, items_per_page=nil
      def total_pages items_qty, items_per_page
        r = items_qty/items_per_page
        r == 0 ? 1 : items_qty % items_per_page !=0 ? r+1 : r
      end

      items_per_page ||= self.klass.respond_to?(:preferences) ? 
        self.klass.preferences.items_per_page.to_i : 10
      items_qty = self.count

      pages_range = 1..total_pages(items_qty, items_per_page)
      offset      = (page_number - 1)*items_per_page
      items_array = self.offset(offset).limit(items_per_page).all

      [ pages_range, items_array ]
    end
  end
end
4

1 回答 1

1

这里有几个问题:

  1. 第一个条目以非 ASCII 字符开头(不确定它到底是什么,但它不是标准的双引号)。我相信那些在标准 ASCII 字符之后排序。

  2. 您似乎希望您的条目从 A 到 Z 排序,但您的 SQL 指定ORDER BY title Desc,它将从 Z 到 A 降序排序。您想要升序排序(ORDER BY title ASC或只是ORDER BY title)。这也是为什么上面的非 ASCII 字符出现在顶部而不是底部的原因。

  3. 它没有指定,但听起来您可能期望排序不区分大小写。SQLite 中的标准整理顺序是 BINARY,它根据 ASCII 值进行排序。由于大写字母的 ASCII 值低于小写字母,因此所有以大写字母开头的标题将排在所有以小写字母开头的标题之前。

阅读SQLite 关于排序序列的文档。为了使标题以不区分大小写的方式排序,您需要使用 NOCASE 整理序列,或者将其添加到 SELECT:

...ORDER BY title COLLATE NOCASE...

或者通过将COLLATE NOCASE语句添加到表定义中的该列中。如果您想将非 ASCII 字符折叠到某处的序列中,您可能需要定义自定义整理序列。

于 2012-10-15T13:09:47.473 回答