Dave
Dave

Reputation: 11862

How do I do this more complex query in Rails?

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

Company 
has_many shareprices

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.

Environment: postgresSQL backend deploying to heroku.

Help very much appreciated.

Upvotes: 0

Views: 28

Answers (1)

rwilliams
rwilliams

Reputation: 21497

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')

Upvotes: 2

Related Questions