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

**URL:** <https://www.ruby-forum.com/t/associations-and-accessors-has-to-be-something-obvious-im/80307>\
**Category:** Rails\
**Created:** [December 23, 2006, 6:55pm UTC](https://www.ruby-forum.com/t/associations-and-accessors-has-to-be-something-obvious-im/80307 "2006-12-23T18:55:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![JL\_Smith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c0e974/32.png) [@JL\_Smith](https://www.ruby-forum.com/u/JL_Smith)\
**Post date:** [December 23, 2006, 6:55pm UTC](https://www.ruby-forum.com/t/associations-and-accessors-has-to-be-something-obvious-im/80307/1 "2006-12-23T18:55:38Z")

</div>

There has to be something obvious I’m overlooking in regards to  
associations and accessors. 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 %\>

 "\> \<%= 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 %\>

I’m trying to 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!

---

<div class="post-metadata">

**Author:** ![JL\_Smith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c0e974/32.png) [@JL\_Smith](https://www.ruby-forum.com/u/JL_Smith)\
**Post date:** [December 23, 2006, 11:32pm UTC](https://www.ruby-forum.com/t/associations-and-accessors-has-to-be-something-obvious-im/80307/2 "2006-12-23T23:32:04Z")

</div>

On Dec 23, 12:55 pm, JL Smith [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> CONSTRAINT `fk_ticket_status` FOREIGN KEY (`status_id`) REFERENCES  
> `ticket_statuses` (`id`)  
> );
> 
> models:  
> Ticket belongs\_to :ticket\_status  
> TicketStatus has\_many :tickets

Ticket belongs\_to :ticket\_status, :foreign\_key =\> “status\_id”  
TicketStatus has\_many :tickets, :foreign\_key =\> “status\_id”

The problem you are facing is because rails can’t infer the foreign key  
properly from the model name TicketStatus (it’s looking for  
ticket\_status\_id).

Best,

-r

> …
> 
> 16: \<%= ticket.ticket\_status.status %\>  
> 17: \<%= ticket.last\_activity\_at %\>  
> 18:   
> 19: \<% end %\>
> 
> I’m trying to 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!
> 
> –  
> Posted viahttp://www.ruby-forum.com/.

–  
Ryan R.  
[http://raaum.org](http://raaum.org)  
[http://rails.raaum.org](http://rails.raaum.org) – Rails docs  
[http://locomotive.raaum.org](http://locomotive.raaum.org) – Self contained Rails for Mac OS X

---

<div class="post-metadata">

**Author:** ![JL\_Smith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c0e974/32.png) [@JL\_Smith](https://www.ruby-forum.com/u/JL_Smith)\
**Post date:** [December 23, 2006, 11:40pm UTC](https://www.ruby-forum.com/t/associations-and-accessors-has-to-be-something-obvious-im/80307/3 "2006-12-23T23:40:05Z")

</div>

Wow, that was easy. Thanks for the help!
