I have a model called Category which looks like this:
class Category < ActiveRecord::Base
has_many :categories
belongs_to :category,:foreign_key =>
I'm not sure I completely understand your question, but category.parent.name
should work. If a category doesn't have a parent, do something like category.parent.try(:name)
to avoid getting a NoMethodError
.
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.children
and category.parent
and also access all the attributes of the asscoiated oobjects,...