Method: ActiveNode::FinderMethods#last

Defined in:
lib/active_node/graph/finder_methods.rb

#last(limit = nil) ⇒ Object

Find the last record (or last N records if a parameter is supplied). If no order is defined it will order by primary key.

Person.last # returns the last object fetched by SELECT * FROM people
Person.where(["user_name = ?", user_name]).last
Person.order("created_on DESC").offset(5).last
Person.last(3) # returns the last three objects fetched by SELECT * FROM people.

Take note that in that last case, the results are sorted in ascending order:

[#<Person id:2>, #<Person id:3>, #<Person id:4>]

and not:

[#<Person id:4>, #<Person id:3>, #<Person id:2>]


155
156
157
158
159
160
161
162
163
164
165
# File 'lib/active_node/graph/finder_methods.rb', line 155

def last(limit = nil)
  if limit
    if order_values.empty?
      order(id: :desc).limit(limit).reverse
    else
      to_a.last(limit)
    end
  else
    find_last
  end
end