从 Rails 中的子对象访问父对象属性 [英] Accessing parent object attribute from child's object in Rails

查看:27
本文介绍了从 Rails 中的子对象访问父对象属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 Category 的模型,如下所示:

class 类别 

活动记录::基础has_many :类别属于_to :category,:foreign_key =>父ID"结尾

我有一个视图,它显示了所有类别及其一些属性.我可以访问 category.parent_id,但我希望能够执行 category.parent_name 之类的操作.
我可以看到自己创建了一个模型方法来获取所有类别并用每个类别的对应父名称填充集合,但我想知道是否有任何方法可以轻松地做到这一点.

我已将模型修改为如下所示:

class 类别 

活动记录::基础has_many :children, :class_name =>'类别', :foreign_key =>'parent_id'属于_to :parent, :class_name =>'类别', :foreign_key =>'parent_id'结尾

创建表类别的迁移是这样的:

类 CreateCategories <ActiveRecord::迁移定义变化创建表:类别做|t|t.string:名称t.text:描述t.integer :parent_idt.timestamps结尾结尾结尾

但是,当我将类别对象传递给视图时,我无法通过执行 category.parent.name 访问其父属性 - 对该对象执行 inspect给我:

如果我检查 category.parent 我会得到:

#

但是,如果我尝试执行 category.parent.name 我会收到以下错误:

nil:NilClass 的未定义方法名称"

我试图访问在我上面提到的对象之前为零的父级.这样做:

category.parent.try(:name)

正如 Michael Irwin 在其中一个答案中所建议的那样解决了它.

解决方案

自引用关联第一次很难...

class 类别 

活动记录::基础has_many :children, :class_name =>'类别', :foreign_key =>'parent_id'属于_to :parent, :class_name =>'类别', :foreign_key =>'parent_id'结尾

然后您可以调用 category.childrencategory.parent 并访问关联对象的所有属性,...

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.

解决方案

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,...

这篇关于从 Rails 中的子对象访问父对象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆