JL Smith
2006-Dec-22 17:12 UTC
Associations and accessors...has to be something obvious I''m doing wrong
I''m quite the Rails beginner and 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 list method: ... 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"><a class="ticket" href="/ticket/show/<%ticket.id %>"><span class="yellow-tag"><%= ticket.id %></span></a></td> <td class="name"><%= h(ticket.title) %></td> <td><%= ticket.ticket_status.status %></td> <td><%= ticket.last_activity_at %></td> </tr> <% end %> ... All I want to do is print out the ticket_status.status field in my list view. I can do this all day: <%= ticket.status_id %>. But when I try <%= ticket.ticket_status.status %> I get the "You have a nil object when you didn''t expect it! The error occured while evaluating nil.status" error. I''ve emptied my tickets table and added all new ones. What stupid thing am I doing wrong? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
JL Smith
2006-Dec-22 21:19 UTC
Associations and accessors...has to be something obvious I''m doing wrong
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") %>"> <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 %> 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! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
JL Smith
2006-Dec-23 16:53 UTC
Associations and accessors...has to be something obvious I''m doing wrong
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") %>"> <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! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2006-Dec-31 00:48 UTC
Re: Associations and accessors...has to be something obvious I''m doing wrong
Hi -- On Sat, 23 Dec 2006, JL Smith wrote:> > 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 :ticketsYou''ve named your foreign key status_id, but ActiveRecord will look for ticket_status_id by default. Try renaming the column. David -- Q. What is THE Ruby book for Rails developers? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf) Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.com) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
JL Smith
2006-Dec-31 05:34 UTC
Re: Associations and accessors...has to be something obvious
> You''ve named your foreign key status_id, but ActiveRecord will look > for ticket_status_id by default. Try renaming the column. > > > DavidI had trouble posting this topic from google groups a WEEK ago and now it''s finally shown up. I gave up on google groups and registered on ruby-forum.com to finally get it posted...something isn''t working right on google groups it would seem. http://www.ruby-forum.com/topic/92132 I appreciate your help though. Thanks. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---