hello folks, i''m playing around with RoR and let''s say I have 2 tables in ny DB: products and categories. each product must have a category. i can associate the category_id field in products table with the different categories that already exist (i used scaffolding). what i cannot do -and i can''t see where the problem is- is to retrieve the category name when i''m displaying my product info. in my app/views/product/list.rhtml i have: <td><%=h product.title %></td> <td><%=h product.category.name %></td> <td><%=h product.price %></td> <td><%=h product.stock %></td> and it keeps saying ''You have a nil object when you didn''t expect it!'' as I want to get product.category.name my models are: class Product < ActiveRecord::Base belongs_to :category end class Category < ActiveRecord::Base has_many :products end the most strange thing of this is that in app/views/product/ _form.rhtml i have: <%= collection_select :product, :category_id, @categories, :id, :name %> and the combo fills up with the categories name! what 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 -~----------~----~----~----~------~----~------~--~---
did you check in the db, that the product record has a valid category_id ? -- 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 -~----------~----~----~----~------~----~------~--~---
yes, it has a valid numerical category_ID that matches what i select in the combo box, actually i can display that ID, but i cannot get that category''s name On Jan 10, 4:25 am, Thorsten Mueller <rails-mailing-l...@andreas- s.net> wrote:> did you check in the db, that the product record has a valid category_id > ? > -- > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---