I'm working through Agile Web Development with Rails and having a problem with Task E and adding the payment types to a database. Within the orders table, there is a foreign key relating the column payment_type_id to the payment_types database (which holds columns id, label and value)
So, an order has a payment_type_id of 1, which corresponds to the entry in payment_types of "Credit Card", "cc"
class Order < ActiveRecord::Base
has_many :line_items
belongs_to :payment_types
class PaymentType < ActiveRecord::Base
has_one :order
Then within the Order controller
class OrdersController < ApplicationController
# GET /orders
# GET /orders.xml
def index
@orders = Order.all
And then in the index.html.erb view:
<% @orders.each do |order| %>
<tr>
<td><%=h order.name %></td>
<td><%=h order.payment_types.label %></td>
This gives the following error:
"You have a nil object when you didn't expect it! The error occurred while evaluating nil.label"
for the line order.payment_types.label
Do I have the relationships correct? The foreign key works elsewhere, when creating orders and everything. It's just in this view. Thanks so much for any help.