I have two tables blogs blog_categories blogs: id category_id ... blog_categories: id name ... in my models I have: class BlogCategory < ActiveRecord::Base has_many :blogs, :foreign_key => "category_id" ### is this needed? end class Blog < ActiveRecord::Base belongs_to :blog_category, :foreign_key => "category_id" end In my template when I try and reference blog.blog_category.name, I get the following error: ActionView::TemplateError (uninitialized constant Blog::BlogCategory) on line #5 of /blogs/list.rhtml: 2: <% for blog in @blogs %> 3: <div class="boxContainerLeftColumn"> 4: <div class="entryTitle"> 5: <%= blog.date_added %> - <%= blog.subject %> - <% if not blog.blog_category.nil? %><%= link_to(blog.blog_category.name, :action => "list", "category_id" => blog.category_id) %><% end %></div> 6: <div class="entryPar"> 7: <!-- need just 300 --> 8: <%= blog.content %> What am I doing wrong? The error message is confusing me somewhat. I tried adding a table_name method to BlogCategory, just in case it wasn''t getting that the table was blog_categories, nothing changed. I know it''s something stupid I am missing.... Thanks!
Demetrius Nunes
2004-Dec-15 09:25 UTC
Re: Question on an error I am getting in ActiveRecord
You are just missing the "require ''blog_category''" on the controller. Sean Leach wrote:>I have two tables > >blogs >blog_categories > >blogs: >id >category_id >... > >blog_categories: >id >name >... > >in my models I have: > >class BlogCategory < ActiveRecord::Base > has_many :blogs, :foreign_key => "category_id" ### is this needed? >end > >class Blog < ActiveRecord::Base > belongs_to :blog_category, :foreign_key => "category_id" >end > >In my template when I try and reference blog.blog_category.name, I get >the following error: > >ActionView::TemplateError (uninitialized constant Blog::BlogCategory) on >line #5 of /blogs/list.rhtml: >2: <% for blog in @blogs %> >3: <div class="boxContainerLeftColumn"> >4: <div class="entryTitle"> >5: <%= blog.date_added %> - <%= blog.subject %> - <% if not >blog.blog_category.nil? %><%= link_to(blog.blog_category.name, :action >=> "list", "category_id" => blog.category_id) %><% end %></div> >6: <div class="entryPar"> >7: <!-- need just 300 --> >8: <%= blog.content %> > > >What am I doing wrong? The error message is confusing me somewhat. I >tried adding a table_name method to BlogCategory, just in case it wasn''t >getting that the table was blog_categories, nothing changed. > >I know it''s something stupid I am missing.... > >Thanks! > > >_______________________________________________ >Rails mailing list >Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >http://lists.rubyonrails.org/mailman/listinfo/rails > > > >
Just FYI: If I remember correctly, in beta gems (and the next version of AR that should be out this week) you don''t need the require statements anymore. When you use has_many or any other association proc, AR will automagically require the needed class files. Very convenient. //jarkko On 15.12.2004, at 11:25, Demetrius Nunes wrote:> You are just missing the "require ''blog_category''" on the controller. >-- Jarkko Laine http://jlaine.net _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails