Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7863431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:28:51+00:00 2026-06-02T23:28:51+00:00

I have 2 records in Foo , with id’s 1 and 2. Both created

  • 0

I have 2 records in Foo, with id’s 1 and 2. Both created in that order. Bare in mind, in Postgres, records have no inherent order.

In Rails console. Foo.first and Foo.last returns the last record. I was under the impression that Foo.first would return the first record.

Here’s the catch. The SQL queries look like:

SELECT "foos".* FROM "foos" LIMIT 1
SELECT "foos".* FROM "foos" ORDER BY "foos"."id" DESC LIMIT 1

The second query (Foo.last) has an ORDER BY DESC. So why doesn’t AR have an ORDER BY ASC for .first? Whats the logic behind this? Seems a bit “inconsistent”.

I can easily solve this by doing: Foo.order('id ASC').first instead. But looking for an explanation.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T23:28:52+00:00Added an answer on June 2, 2026 at 11:28 pm

    There isn’t any logic to it, if there was any sense to first (or last for that matter), then it would raise an exception if you neglected to specify an explicit order either as an argument to first or as part of the current scope chain. Neither first nor last make any sense whatsoever in the context of a relational database unless there is an explicit ordering specified.

    My guess is that whoever wrote first assumed that order by whatever_the_pk_is was implicit if there was no explicit order by. Then they probably did some experiments to empirically verify their assumption and it just happened to work as they expected with the particular tables and databases that they checked with (mini-rant: this is why you never ever assume unspecified behavior; if a particular behavior isn’t explicitly specified, don’t assume it even if the current implementation behaves that way or if empirical evidence suggests that it behaves that way).

    If you trace through a simple M.first, you’ll find that it does this:

    limit(1).to_a[0]
    

    No explicit ordering so you get whatever random ordering the database feels like using, that could be order by pk or it could be the table’s block order on disk. If you trace through M.last, you’ll get to find_last:

    def find_last
      #...
            reverse_order.limit(1).to_a[0]
      #...
    end
    

    And reverse_order:

    def reverse_order
      relation = clone
      relation.reverse_order_value = !relation.reverse_order_value
      relation
    end
    

    The @reverse_order_value instance variable isn’t initialized so it will start out as nil and a ! will turn it into a true. And if you poke around for how @reverse_order_value is used, you’ll get to reverse_sql_order:

    def reverse_sql_order(order_query)
      order_query = ["#{quoted_table_name}.#{quoted_primary_key} ASC"] if order_query.empty?
      #...
    

    and there’s the author’s invalid assumption about ordering laid bare for all to see. That line should probably be:

    raise 'Specify an order you foolish person!' if order_query.empty?
    

    I’d recommend that you always use .order(...).limit(1).first instead of first or last so that everything is nice and explicit; of course, if you wanted last you’d reverse the .order condition. Or you could always say .first(:order => :whatever) and .last(:order => :whatever) to again make everything explicit.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The problem: I have a table that records data rows in foo . Each
I have records in the database with the sort order number. Now I want
I have two records: -record(foo, {timestamp, name}). -record(bar, {timestamp, name}). And I would like
I have a binary file that was created on a unix machine. It's just
I have an application that uses WebServices to send database records (as an array)
I have quite a few records in my program that I end up putting
I have a very large dataset - millions of records - that I want
I have a web form that allows users to search on and edit records
I have a table that records a sequence of actions with a field that
Let's assume that I have two tables... Foo and Bar. They contain the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.