【问题标题】:Mongoid Rails 4 sort by asc or desc order created_atMongoid Rails 4 按 asc 或 desc order created_at 排序
【发布时间】:2014-07-25 01:35:54
【问题描述】:

我有一个使用 Mongoid 的 rails 4 应用程序。 我想做一些基本的事情是根据索引视图中的 created_at 字段按降序显示我拥有的书籍模型。 在控制器books_controller.rb中:

def index
  @books = Book.order_by(:created_at.desc)
end

这不起作用。我还尝试了以下两种不起作用:

@books = Book.find :all, :order => "created_at DESC"

Book.find(:all, :order => "created_at DESC").each do |item|
  @books << item
end

在视图中我有这样的东西:

<% @books.each do |b| %>
  ...
<% end %>

谢谢。

【问题讨论】:

  • 什么意思?你有什么错误吗?

标签: sorting date ruby-on-rails-4 mongoid


【解决方案1】:
def index
  @books = Book.desc(:created_at)
end

效果很好。

【讨论】:

    【解决方案2】:

    你可以试试这个

    def index
      @books = Book.order_by(created_at: :desc)
    end
    

    效果很好。

    【讨论】:

    • 小改进:@books = Book.order_by(created_at: :desc)
    【解决方案3】:

    Book.where(author: "Stephen King").sort({"created_at": 1})--> 升序

    Book.where(author: "Stephen King").sort({"created_at": -1}) --> 降序

    【讨论】:

    • 语法错误,加上sort 似乎不是现有方法。
    • 编辑了语法错误。使用 mongoid gem 时可以使用排序选项。立即尝试。
    • 感谢您的编辑。 Book.sort({"created_at": 1}) 对我仍然无效,但 Book.where(author: "Stephen King").sort({"created_at": 1}) 有效。
    【解决方案4】:

    您可以同时使用“order”和“order_by”,它们是等价的。 所有这些都是等价的:

    Book.order_by(created_at: :desc)
    Book.order_by(created_at: -1)
    Book.order(created_at: :desc)
    Book.order(created_at: -1)
    

    这是来自 mongoid 5.1.3 "lib/mongoid/criteria/queryable/optional.rb" 的源代码:

    # Adds sorting criterion to the options.
    #
    # @example Add sorting options via a hash with integer directions.
    #   optional.order_by(name: 1, dob: -1)
    #
    # @example Add sorting options via a hash with symbol directions.
    #   optional.order_by(name: :asc, dob: :desc)
    #
    # @example Add sorting options via a hash with string directions.
    #   optional.order_by(name: "asc", dob: "desc")
    #
    # @example Add sorting options via an array with integer directions.
    #   optional.order_by([[ name, 1 ], [ dob, -1 ]])
    #
    # @example Add sorting options via an array with symbol directions.
    #   optional.order_by([[ name, :asc ], [ dob, :desc ]])
    #
    # @example Add sorting options via an array with string directions.
    #   optional.order_by([[ name, "asc" ], [ dob, "desc" ]])
    #
    # @example Add sorting options with keys.
    #   optional.order_by(:name.asc, :dob.desc)
    #
    # @example Add sorting options via a string.
    #   optional.order_by("name ASC, dob DESC")
    #
    # @param [ Array, Hash, String ] spec The sorting specification.
    #
    # @return [ Optional ] The cloned optional.
    #
    # @since 1.0.0
    def order_by(*spec)
      option(spec) do |options, query|
        spec.compact.each do |criterion|
          criterion.__sort_option__.each_pair do |field, direction|
            add_sort_option(options, field, direction)
          end
          query.pipeline.push("$sort" => options[:sort]) if aggregating?
        end
      end
    end
    alias :order :order_by
    

    【讨论】:

      【解决方案5】:
      def index
         @books = Book.order(:created_at => 'desc')
      end
      

      它对我有用,而不是 order_by 使用 order(取决于 rails 版本)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-07-11
        • 1970-01-01
        • 2019-08-13
        • 2011-06-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多