Rails console—无法访问对象的属性(虽然我可以访问对象本身)

Rails console—Can't access object's attributes (though I can access the object itself)

我在 userorganisation 之间建立了 has_many through 关系,org_access table 加入了他们。

在 Rails 控制台中输入:

user = User.first
org_access = user.org_access

然后打印出来:

=> [#<OrgAccess:0x007fe06632aa20 id: 1, organisation_id: 1, user_id: 1, access_status: 0, role: 0>]

但如果我尝试:

org_access.role
user.role
user.organisation.role

None 这些命令将 return role 字段中的 org_access。如何访问用户+组织的 org_access 字段?

org_access 是一个 数组 has_many 关联始终表示 0 个或多个项目。

你需要org_access[0].role等等