访问 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.用户有_一个家务,家务属于用户
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 %>
很遗憾,我无法访问 Chore 的 name 属性.我收到此错误:
Unfortunately, I cannot access the name attribute of the Chore. I get this error:
nil:NilClass 的未定义方法 `name'
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.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 的未定义方法 `name'
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屋!