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 6759939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:00:37+00:00 2026-05-26T14:00:37+00:00

Not understanding how to express a particular type of query with ActiveRecord’s syntax. Company

  • 0

Not understanding how to express a particular type of query with ActiveRecord’s syntax.

Company 
has_many shareprices
  • When the processes run to update the shareprices, a new entry is created in the shareprice table. So you obviously end up with lots of shareprice rows for each company. In such a scenario you could end up with 1 company having 5 very high share price entries in the last time period.

So let’s say I want to return the companies that currently have the highest shareprice in the last time period – I need it to basically return the max price for that company from the shareprices table and then find the next.

  • I can’t work out how to do that with ActiveRecord syntax. I’ve tried a lot of approaches inspired by other stackoverflow answers but invariably can’t get it to return unique companies so I’m missing a point somewhere around select unique, joining, group by, or something else.

Environment: postgresSQL backend deploying to heroku.

Help very much appreciated.

  • 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-26T14:00:37+00:00Added an answer on May 26, 2026 at 2:00 pm

    Without knowing your structure it’s hard to provide a concrete answer. But somthing like this should work.

    Company.select('max(share_prices.price) as price,companies.id').joins(:share_prices).group(:id).order('price')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a bit not understanding - on update, insert, delete queries result_metadata returns false?
Not understanding why my commit to remote git repo is not working. So I
Im not understanding something about rest web services. When you do get, post etc
I'm not understanding how OAuth2 works for Google API. Supposing I've signed in API's
I have some not understanding actions from gnu clisp Suppose, I have some code
I am not understanding the 'aligning allocated memory' part from the mprotect usage. I
I'm not understanding how Twitter Bootstrap does active links for the navigation. If I
I still not understanding the parent role in the android styles I have put
Either I am missing something or not understanding other posts/instructions. I am trying to
I've read this answer about eight-five times, but there's something I'm not understanding correctly:

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.