Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7067341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:11:26+00:00 2026-05-28T05:11:26+00:00

I have a model called Category which looks like this: class Category < ActiveRecord::Base

  • 0

I have a model called Category which looks like this:

class Category < ActiveRecord::Base
  has_many :categories
  belongs_to :category,:foreign_key => "parent_id"
end

I have a view which shows all the categories with some of their attributes. I can access category.parent_id, but I would like to be able to do something like category.parent_name.
I can see myself creating a model method to fetch all categories and filling the collection with the correspondent parent name of each category, but I’m wondering if there is anyway to do this easily.

EDIT: I have modified the model to have it like this:

class Category < ActiveRecord::Base
  has_many :children, :class_name => 'Category', :foreign_key => 'parent_id'
  belongs_to :parent, :class_name => 'Category', :foreign_key => 'parent_id'
end

The migration to create the table categories is like this:

class CreateCategories < ActiveRecord::Migration
  def change
    create_table :categories do |t|
      t.string :name
      t.text :description
      t.integer :parent_id

      t.timestamps
    end
  end
end

However when I pass a category object to a view I am not able to access its parent attributes by doing category.parent.name – Doing an inspect of that object gives me:

<Category id: 2, name: "Test 2", description: "Prova 2", parent_id: 1, created_at: "2012-01-17 19:28:33", updated_at: "2012-01-17 19:28:33">

And if I do an inspect of category.parent I get this:

#<Category id: 1, name: "Prova", description: "Test", parent_id: nil, created_at: "2012-01-17 19:28:17", updated_at: "2012-01-17 19:28:17">

However if I try to do category.parent.name I get the following error:

undefined method `name' for nil:NilClass

EDIT2: I was trying to access a parent that was nil before the object that I mentioned above. Doing this:

category.parent.try(:name) 

as suggested by Michael Irwin in one of the answers solved it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:11:26+00:00Added an answer on May 28, 2026 at 5:11 am

    Self referencing associations are hard at the first time…

    class Category < ActiveRecord::Base
      has_many :children, :class_name => 'Category', :foreign_key => 'parent_id'
      belongs_to :parent, :class_name => 'Category', :foreign_key => 'parent_id'
    end
    

    Then you could call category.childrenand category.parent and also access all the attributes of the asscoiated oobjects,…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model called Contact which has_one guest like so. class Contact <
I have a model called company that has_many users then users belongs_to company. class
I have a model called SimplePage in which I have this line: category =
I have a model called Category in my app in GAE. This model simply
I have a model category which contains a field called category.parent_id which is used
I have a Model called Category and other Model Product. They have has_many and
I have a method in codeigniter that looks like this, public function category() {
I have a model that looks like this: Ext.regModel('TreeItem', { fields: [ { name:
I am using Hibernate with spring. I have a model-class like this. @Entity @Table(name
In our Core domain model design, we have got a class called Category whose

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.