访问has_one关联的属性 [英] Accessing a has_one associations' attributes
问题描述
我对Rails还是很陌生,所以希望这不是一个愚蠢的问题.
I'm still quite new to Rails so hopefully this isn't a silly question.
我有两个模型:User和Chore.用户has_one琐事,该杂事属于User
I have two models: User and Chore. User has_one chore, and Chore belongs to User
class User < ActiveRecord::Base
attr_accessible :chore_done, :email, :name, :phone
has_one :chore
class Chore < ActiveRecord::Base
attr_accessible :name, :user_id
belongs_to :user
在我的用户索引中,我试图显示所有用户并显示与他或她相关联的琐事.我这样做是通过将User.all传递到视图,并使用.each遍历每个用户:
In my user index, I'm trying to show all users and display the chore that is associated with him or her. I'm doing this by passing User.all to the view and using a .each to iterate through each user:
<% @users.each do |user| %>
<tr>
<td><%= user.name %></td>
<td><%= user.chore.name %></td>
<td><%= user.chore_done? %></td>
<td><%= link_to 'Edit', edit_user_path(user) %></td>
<td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
不幸的是,我无法访问杂项的名称属性.我收到此错误:
Unfortunately, I cannot access the name attribute of the Chore. I get this error:
nil:NilClass的未定义方法名称"
undefined method `name' for nil:NilClass
如果删除.name属性,它将仅返回指向Chore对象的指针.
If I remove the .name attribute, it just returns a pointer to the Chore object.
我感觉这与将User.all对象传递给视图,然后对其进行迭代有关.只需在控制台中访问特定的User对象(例如User.find(1)),然后访问user.chore.name即可.
I have a feeling this has something to do with passing the User.all object to the view and then iterating over that. Just accessing a specific User object (e.g. User.find(1)) in the console, and then accessing user.chore.name works fine.
1.9.3-p194 :045 > user = User.find(4)
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 4]]
=> #<User id: 4, name: "Example User", email: "example@dot.com", phone: 8675309, chore_done: false, created_at: "2012-11-05 01:53:33", updated_at: "2012-11-05 01:53:33">
1.9.3-p194 :046 > user.chore.name
Chore Load (0.3ms) SELECT "chores".* FROM "chores" WHERE "chores"."user_id" = 4 LIMIT 1
=> "Living room"
我在APIDock中阅读了一些有关AssociationProxy的内容,但是我认为我并不完全了解它,也不知道如何使用它.从我收集的数据来看,访问模型中的所有对象都会返回一个对象数组,但不会为其依赖项返回完整的属性集吗?因此,在这种情况下,我获得了指向Chore对象的指针,但无法访问其任何属性.
I read a bit about AssociationProxy in the APIDock, but I don't think I understand it completely nor do I know how to work with it. From what I gathered, accessing all objects in a model returns an Array of objects but doesn't return a complete set of attributes for its dependencies? So, in this case, I get a pointer to the Chore object but no access to any of its attributes.
我当然可以将Chore添加为用户表中的另一列,但将来可能会添加到该模型中+我只想弄清楚has_one关联的工作方式.
I could certainly just add Chore as another column in my user table, but I may add to that model in the future + I just want to figure out how the has_one association works anyway.
感谢您的帮助!
推荐答案
nil:NilClass的未定义方法名称"
undefined method `name' for nil:NilClass
这表示您正在尝试在Nil
的实例上调用某些方法name()
.在您的代码上下文中,这表示此行上的chore
是nil
This says that you are trying to call some method name()
on an an instance of Nil
. Within your code's context, that says that chore
on this line is nil
<td><%= user.chore.name %></td>
简单地说,@user
中的User
实例之一没有关联的Chore
.在您的视图中适应此问题的一种简单方法是检查user.chore
是否存在.
Simply put, one of the User
instances in @user
has no associated Chore
. One simple way to accomodate this in your view is by checking if user.chore
exists.
<td><%= user.chore.nil? ? "some default name" : user.chore.name %></td>
这篇关于访问has_one关联的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!