Forum: Ruby on Rails Associations and accessors...has to be something obvious I'm

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
JL Smith (Guest)
on 2006-12-23 19:55
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 %>
   <tr class="<%= cycle("list-line-odd", "list-line-even") %>">
       <td class="name"><%= link_to ticket.id, { :action => 'show',
:id => ticket }, :class => 'ticket-yellow' %></td>
       <td class="name"><%= h(ticket.title) %></td>
       <td><%= ticket.ticket_status.status %></td>
       <td><%= ticket.last_activity_at %></td>
   </tr>
<% 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:         <td class="name"><%= link_to ticket.id, { :action => 'show',
:id => ticket }, :class => 'ticket-yellow' %></td>
15:         <td class="name"><%= h(ticket.title) %></td>
16:         <td><%= ticket.ticket_status.status %></td>
17:         <td><%= ticket.last_activity_at %></td>
18:     </tr>
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!
(Guest)
on 2006-12-24 00:32
(Received via mailing list)
On Dec 23, 12:55 pm, JL Smith <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:         <td><%= ticket.ticket_status.status %></td>
> 17:         <td><%= ticket.last_activity_at %></td>
> 18:     </tr>
> 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://rails.raaum.org -- Rails docs
http://locomotive.raaum.org -- Self contained Rails for Mac OS X
JL Smith (Guest)
on 2006-12-24 00:40
(Received via mailing list)
Wow, that was easy.  Thanks for the help!
This topic is locked and can not be replied to.