Вопрос

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.

Это было полезно?

Решение

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)
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top