I have a model called Category which looks like this:
我有一个名为Category的模型,如下所示:
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.
我有一个视图,显示所有类别及其一些属性。我可以访问category.parent_id,但我希望能够做类似category.parent_name的事情。我可以看到自己创建一个模型方法来获取所有类别并使用每个类别的对应父名称填充集合,但我想知道是否有任何方式可以轻松地执行此操作。
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.parent.name来访问其父属性 - 对该对象进行检查可以让我:
<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.parent我得到这个:
#<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:
但是,如果我尝试执行category.parent.name,则会收到以下错误:
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:
EDIT2:我试图在上面提到的对象之前访问一个nil的父级。这样做:
category.parent.try(:name)
as suggested by Michael Irwin in one of the answers solved it.
正如Michael Irwin所建议的那样,其中一个答案解决了它。
2 个解决方案
#1
12
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,...
然后你可以调用category.children和category.parent,也可以访问asscoiated oobjects的所有属性,...
#2
6
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
.
我不确定我是否完全理解你的问题,但category.parent.name应该有效。如果某个类别没有父类,请执行category.parent.try(:name)之类的操作以避免获取NoMethodError。
#1
12
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,...
然后你可以调用category.children和category.parent,也可以访问asscoiated oobjects的所有属性,...
#2
6
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
.
我不确定我是否完全理解你的问题,但category.parent.name应该有效。如果某个类别没有父类,请执行category.parent.try(:name)之类的操作以避免获取NoMethodError。