Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in SQL by (20.3k points)

Currently, I can make the straight-up SQL query on my DB:

SELECT MAX(bar) FROM table_name

And it returns with the max value in that table. When I make what I consider to be an equivalent call in Rails, however, it does not work. I am calling:

Bar.all(:select => "Max(bar)")

This simply returns with:

[#<Bar >]

In the column I'm calling on is a series of identifying numbers, I'm looking for the largest one. Is there some other way of accessing this in Rails?

1 Answer

0 votes
by (40.4k points)

It looks like your model name is Bar and it has a column named bar, the below code will work:

Bar.maximum("bar")

For more information, you can refer to Rails Guides section on Calculations.

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.2k users

Browse Categories

...