mardi 24 février 2015

Ruby on Rails method to count multiple fields using where

In my Ruby on Rails application I am creating a cinema system, and when a user is logged on they can click "My Bookings" which will list all of their bookings. (Note, when a user books a showing they choose the amount of adult seats, child seats, senior seats, and student seats). On this page I want to display the amount of seats the user booked for that showing, so rather than saying adult seats: 1, child seats: 3, senior seats: 2, it would display seats booked: 6.


I have started to create a method to do this in booking.rb model:



def user_booked_seats_quantity_for_showing
user = User.find(session[:user_id]).id
showing = booking.showing_id
self.where("user_id = ? and showing_id = ?", user, showing)
end


But am unsure how to get the method to add up the data in the attributes: adult_seats, child_seats, senior_seats, and student_seats, where the user_id is the user currently logged in and the showing_id is that particular showing.


Can anyone help?


It is also worth noting that this is for the views/bookings/index so if a user has multiple bookings it will list all of them.


Aucun commentaire:

Enregistrer un commentaire