Home > DeveloperSection > Forums > Saving order value to each booking
Ashish Pandey

Total Post:128

Points:898
Posted on    December-28-2014 11:53 PM

 Ruby on Rails Ruby on Rails  Ruby 
Ratings:


 1 Reply(s)
 688  View(s)
Rate this:
I'm creating a ticket booking app as my sample project using Ruby on Rails 4.1. Three are three models - Events, Tickets and Bookings. Events have many tickets and bookings. Tickets have many bookings and they belong to events. Bookings belongs to events and tickets.

Tickets model has the price field and bookings model has the order_quantity field. Right now, I use a model method total_amount in bookings.rb to display the total price ie. (price * order_quantity) in the bookings show page (@booking.total_amount).

Now, in the bookings index page, I use a block to list all the details Buyer name, ticket type, order quantity etc. Here, I'd like to show the total amount as well. What's the best way to do it? Do I need to create a total_price column in the bookings table and save the amount in the create action?

Bookings Index page looks like this:

<% @event.bookings.each do |booking| %>
              <td><%= booking.buyer_name %></td>
              <td><%= booking.email %></td>
              <td><%= booking.ticket_id %></td>
              <td><%= booking.order_quantity %></td>
<% end%>


Manoj Bhatt
Manoj Bhatt

Total Post:153

Points:1079
Posted on    December-29-2014 12:13 AM

ActiveRecord will return a collection of model instances when you access the relationship in this way. In other words, when you cycle through the bookings, each one is an instance of your model.

For this reason, you can just re-use your method:

<td><%= booking.total_amount %></td>

Don't want to miss updates? Please click the below button!

Follow MindStick