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

  • SEARCH
  • Home
  • 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 7658313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:11:09+00:00 2026-05-31T13:11:09+00:00

I have an ActiveRecord relationship between Trade and Execution . I can get Trade.executions

  • 0

I have an ActiveRecord relationship between Trade and Execution. I can get

Trade.executions  #returns all exeuctions realated to the Trade

If I do

Trade.executions.last

It seems seems to return the last execution record based on ID.

Is this the correct way to retrieve the last execution record related to Trade based on ID?

  • 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-05-31T13:11:09+00:00Added an answer on May 31, 2026 at 1:11 pm

    No, that’s not guaranteed to give you the Execution with the highest id. If you don’t specify an explicit ordering then the records can come out of the database in any order. The fact that they look like they’re sorted by id is just a convenient accident.

    You should do one of these:

    highest_id_execution = trade.executions.order(:id).last
    highest_id_execution = trade.executions.order('id desc').first
    

    That will give you the execution for trade that has the highest id. If you really want the most recently created one then you should order(:created_at) instead:

    most_recent_execution = trade.executions.order(:created_at).last
    most_recent_execution = trade.executions.order('created_at desc').first
    

    The id and created_at columns will almost always come in the same order but you should say what you mean to make things clearer to the people that maintain your code.

    In both cases, the order(:x).last and order('x desc').first are exactly the same and even resolve to exactly the same SQL so use whichever one makes the most sense to you.

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

Sidebar

Related Questions

Let's say I have many-to-many relationship (using the ActiveRecord attribute HasAndBelongsToMany) between Posts and
Let's say I have a many-to-many relationship between users and group. A user can
I have a simple has_many/belongs_to relationship between Report and Chart. The issue I'm having
I have three models Article , Author , and AuthorLine presenting relationship between article
I have two models, Project and Category, which have a many-to-many relationship between them.
I have the following two models: School and User, and a HABTM relationship between
I have a has_many relationship between two entities, Feeds and Posts. I also have
I have a standard many-to-many relationship between users and roles in my Rails app:
I have a has_many :through relationship set up like so class Situation < ActiveRecord::Base
I have many-to-many relationship between Game and Account models like below: class Account <

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.