Women in Technology

Hear us Roar



Article:
  Rolling with Ruby on Rails
Subject:   recipe.category.name does not work for me
Date:   2005-02-07 10:36:54
From:   pbihari
The line <%= recipe.category.name %> does not work for me. I get the following error:

Showing /recipe/list.rhtml where line #18 raised undefined method `name' for nil:NilClass
Full Threads Oldest First

Showing messages 1 through 7 of 7.

  • Curt Hibbs photo recipe.category.name does not work for me
    2005-02-07 11:45:53  Curt Hibbs | O'Reilly AuthorO'Reilly Blogger [View]

    Sounds like you didn't include the relationship statements in your models. Make sure your Recipe class looks like this:


    class Recipe < ActiveRecord::Base
    belongs_to :category
    end


    And your Category class looks like this:


    class Category < ActiveRecord::Base
    has_many :recipes
    end


    • recipe.category.name does not work for me
      2007-02-14 18:01:44  aussierob [View]

      it didnt work for me either!
      BUT THEN I CHECKED and RE-CHECKED and found I had my class 'has-many' and 'belong-to' around the wrong way!
      I guess that is the ONE thing this wonderfull tutorial cannot anticipate - the obscure human errors that result in most of these postings!

      So to all those with problems - keep looking !

      and to those that created this GREAT WORK thanks you a lot from down under,

      and to all those that help - goodonya too!

      regards to ALL, Robert.
    • recipe.category.name does not work for me
      2005-02-09 01:59:22  pbihari [View]

      My Recipe and Category model classes are exactly the same as your code sample above. There could be something else I could be doing wrong. But I can't seem to figure it out. I have matched my code word for word with your example, but something seems to be cheating my eye.
      • Curt Hibbs photo recipe.category.name does not work for me
        2005-02-09 03:17:07  Curt Hibbs | O'Reilly AuthorO'Reilly Blogger [View]

        Hmmm... I think the fastest way to figure this out is to get on the rails irc channel (I'm not on my own computer, so you'll have to find the info on the rails web site), or if you're not in a rush post as much info as possible to the rails mailing list.
        • recipe.category.name does not work for me
          2005-02-09 04:55:59  pbihari [View]

          I got it to work now. The error seemed to stem from the fact that there was at least one recipe without a category assigned to it. However, in my table definition for "recipes", I had defined "category_id" to be a NULL ALLOWED category. So I thought that it should work. Is that a problem, in general, to have NULL in foreign key fields while working with Rails?
          • Curt Hibbs photo recipe.category.name does not work for me
            2005-02-09 05:43:08  Curt Hibbs | O'Reilly AuthorO'Reilly Blogger [View]

            The problem with NULL is not the database, its the code that I wrote, which does not check for a null value. In list.rhtml the value of "recipe.category" is normally a Category object that represents that recipe's row in the catgories table.

            If a recipe has NULL in the database for its category_id, then the value of "recipe.category" would be "nil" (this is the Ruby's constant for NULL). When the code says "recipe.category.name" it tries to invoke the "name" method on "recipe.category" which is fine if recipe.category is a real object, but if it is "nil", then an exception is thrown because "nil" does not have a "name" method.

            If you wanted to allow NULL categories, then the code would have to check for this.
            • recipe.category.name does not work for me
              2005-02-09 06:14:22  pbihari [View]

              Thanks a lot Curt for your explanation and for your readiness in providing support!