I am looking to get a the number of children that meet certain criteria (is_confirmed, in this instance.) This is returned in get_confirmed_amount:
def get_confirmed_amount(reservations)
amount = 0
reservations.each do |reservation|
if reservation.is_confirmed
reservation.persons.each do
amount = amount + 1
end
end
end
end
I then calling this from the index action:
def index
@reservations = Reservation.where(customer_id: session[:customer_id]).order(:name)
confirmed_amount = get_confirmed_amount(@reservations)
end
I then need to pass this information to the view, but setting it as @reservations.confirmed_amount will return NoMethodError. How can I get the information to the view without having to send two instance variables to the view, or is that the only way?
get_confirmed_amountisn't returning a count butreservations. Instance variables and/or instance methods are the only way to send it to the view@reservations.confirmed_amountis@reservationsisn't aReservationobject. It's anActiveRecord Collection. There may be a way to add a method to ActiveRecord Collections, but I don't know it.confirmed_amount?cuttheget_confirmed_amountmethod and then paste it to thehelperfile of the currentcontroller. Then from view call for the method with<%= get_confirmed_amount(@reservations) %>