Associations and accessors...has to be something obvious I'm


#1

I’m quite the Rails beginner because I’m trying to do something pretty
fundamental with associations. I think I just need a good shove in the
right direction. TIA!

schema:
CREATE TABLE ticket_statuses (
id int(11) NOT NULL,
status varchar(255) default NULL,
PRIMARY KEY (id)
);

CREATE TABLE tickets (
id int(11) NOT NULL,
title varchar(255) default NULL,
details text,
status_id int(11) default NULL,
opened_at datetime default NULL,
closed_at datetime default NULL,
last_activity_at datetime default NULL,
PRIMARY KEY (id),
KEY fk_ticket_status (status_id),
CONSTRAINT fk_ticket_status FOREIGN KEY (status_id) REFERENCES
ticket_statuses (id)
);

models:
Ticket belongs_to :ticket_status
TicketStatus has_many :tickets

ticket controller:

def list
@ticket_pages, @tickets = paginate :tickets, :order => “id DESC”,
:per_page => 10
end

ticket list view:

<% for ticket in @tickets %>
<tr class="<%= cycle(“list-line-odd”, “list-line-even”) %>">

<%= link_to ticket.id, { :action => ‘show’,
:id => ticket }, :class => ‘ticket-yellow’ %>
<%= h(ticket.title) %>
<%= ticket.ticket_status.status %>
<%= ticket.last_activity_at %>

<% end %>

error:
You have a nil object when you didn’t expect it!
The error occured while evaluating nil.status

Extracted source (around line #16):

13: <tr class="<%= cycle(“list-line-odd”, “list-line-even”) %>">
14:

<%= link_to ticket.id, { :action =>
‘show’, :id => ticket }, :class => ‘ticket-yellow’ %>
15: <%= h(ticket.title) %>
16: <%= ticket.ticket_status.status %>
17: <%= ticket.last_activity_at %>
18:
19: <% end %>

All I want to do is display the ticket’s actual status, not the
status_id. Do I have an association setup incorrectly? Am I breaking
a naming convention or something? Thanks for any help!