Need help converting SQL query to Ruby.

110 Views Asked by At

I'm new to Ruby on Rails. I'm trying to determine the proper ruby query for the following SQL query.

Select max(bid_amount) from biddings where listing_id = 1;

I need to extract the maximum value in the bid_amount column. But it has to have a dynamic listing_id.

1

There are 1 best solutions below

4
On BEST ANSWER

Try:

Bidding.where('listing_id = :listing_id', listing_id: 1).maximum(:bid_amount)

Update:

To follow up on your comment: since you say you are passing in params[:id], it's best to convert that parameter to integer so that unwanted values don't go to the database. For e.g.

Bidding.where('listing_id = :listing_id', listing_id: params[:id].to_i).maximum(:bid_amount)