Method: Sequel::Dataset#last

Defined in:
lib/sequel/dataset/actions.rb

#last(*args, &block) ⇒ Object

Reverses the order and then runs #first with the given arguments and block. Note that this will not necessarily give you the last record in the dataset, unless you have an unambiguous order. If there is not currently an order for this dataset, raises an Error.

DB[:table].order(:id).last # SELECT * FROM table ORDER BY id DESC LIMIT 1
# => {:id=>10}

DB[:table].order(Sequel.desc(:id)).last(2) # SELECT * FROM table ORDER BY id ASC LIMIT 2
# => [{:id=>1}, {:id=>2}]

Raises:



475
476
477
478
# File 'lib/sequel/dataset/actions.rb', line 475

def last(*args, &block)
  raise(Error, 'No order specified') unless @opts[:order]
  reverse.first(*args, &block)
end