A
A
Alexander Petrov2017-03-28 12:23:59
Ruby on Rails
Alexander Petrov, 2017-03-28 12:23:59

How to display the minimum price in a group?

I read here rusrails.ru/active-record-query-interface 7th point, but I didn’t understand something ....
There is a Room model. They have a plan_id parameter. You need to group them by plan_id and display the cheapest one from the group.
Right now I'm doing this: Room.order(price: :asc).group("plan_id"). If you put desc, then it displays the most expensive one, and if asc, then the logic is not clear at all. As I understand it, somewhere you need to specify min (price) somewhere, but I don’t understand where and how.
Help, people! Thanks in advance)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
shagguboy, 2017-03-28
@shagguboy

www.sql.ru/forum/687908/faq-vyborka-pervoy-posledn...

V
vsuhachev, 2017-03-29
@vsuhachev

Room.group(:plan_id).minimum(:price)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question