techquestion
techquestion

Reputation: 489

How to rank based on a selection?

For a reservation I would like to calculate the price for extra_guests (e.g. guests not included in the price).

The goal is to use the ranking to start calculating the extra_guest_price of the extra_guest with the lowest age_table ranking, in case there are guests not included in the standard reservation.

Issue

I just need a specific range of age_tables to rank based on :rank. My current set-up is wrong as I cannot use order on an array, but I'm not sure how to structure it to make it work.

def total_extra_guest_price(reservation, res_guest)
    sum_guest = 0
    sum_amount_guests = 0
    age_table_list = []
    reservation.reservation_extra_guests.each do |guest|
      sum_amount_guests += guest.extra_guest_quantity
    end

    # persons included vs persons reservation
     #if more persons
    if reservation.room.room_category.persons_included < sum_amount_guests

      #count number of too many persons
      extra_persons = sum_amount_guests - reservation.room.room_category.persons_included

      # iterate over extra_guests belonging to reservation to get age_tables
      reservation.reservation_extra_guests.each do |extra_guest_item|
        age_table_list << extra_guest_item.extra_guest.age_table
      end

      #rank age tables, so lowest rank can be used to calculate price. Issue!!!
      age_table_list.order(:rank)
    end
end

Upvotes: 0

Views: 37

Answers (1)

Thang
Thang

Reputation: 841

I'm not 100% clear about your question so my suggestion may not be the best solution. But you can try the sort_by()

age_table_list.sort_by{ |age_table| age_table.rank }

read more here

Upvotes: 1

Related Questions