40

I have a rails 4 app using Mongoid. I want to do something basic is display the book model I have by descending order according to the field created_at in the index view. In the controller books_controller.rb:

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

This is not working. I also tried the following 2 that are not working:

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

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

In the view i have something like this:

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

Thank you.

Shahzad Tariq
  • 2,767
  • 1
  • 22
  • 31
Chleo
  • 935
  • 2
  • 8
  • 14

5 Answers5

66

You can try this

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

it works fine.

Lee Dykes
  • 1,017
  • 8
  • 16
Shahzad Tariq
  • 2,767
  • 1
  • 22
  • 31
4
def index
   @books = Book.order(:created_at => 'desc')
end

It works for me, instead of order_by use order(depends on rails version)

pbms
  • 586
  • 1
  • 10
  • 32
3

You can use both "order" and "order_by", and they are equivalent. All these are equivalent:

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

This is the source code from 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
liukgg
  • 226
  • 3
  • 6
3

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

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

Vinu Joseph
  • 965
  • 9
  • 11
1
def index
  @books = Book.desc(:created_at)
end

It works fine.

Shekhar Patil
  • 337
  • 4
  • 14