在Rails中从子对象访问父对象属性 [英] Accessing parent object attribute from child's object in Rails
问题描述
class Category< p> ActiveRecord :: Base
has_many:categories
belongs_to:category,:foreign_key => parent_id
end
我有一个视图,它显示了所有的类别属性。我可以访问 category.parent_id
,但我希望能够做一些像 category.parent_name
。 >
我可以看到自己创建了一个模型方法来获取所有类别,并用每个类别的记录父类名称来填充集合,但是我想知道是否有这样的方法。
编辑:我修改了模型,使其具有这样的效果:
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
创建表类别的迁移过程如下所示: p>
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
结束
但是,当我传递一个类别对象到一个视图,我无法访问其父通过执行 category.parent.name
- 做一个 inspect
这个对象给我:
< 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>
如果我检查 category.parent $ c $
#< 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>
但是如果我试着做 category.parent.name
未定义方法`name'为nil:NilClass
编辑2:我试图访问父对象,我上面提到的零。这样做:
category.parent.try(:name)
$ p
由 Michael Irwin 中的一个答案解决了它。
自引用关联在第一次很难...
class类别< ActiveRecord :: Base
has_many:children,:class_name => 'Category',:foreign_key => 'parent_id'
belongs_to:parent,:class_name => 'Category',:foreign_key => 'parent_id'
end
然后你可以调用
category.children
和category.parent
,并且还可以访问asscoiated对象的所有属性,...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 likecategory.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 aninspect
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.
解决方案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
andcategory.parent
and also access all the attributes of the asscoiated oobjects,...这篇关于在Rails中从子对象访问父对象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文