Mirror318
Mirror318

Reputation: 12693

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

I have a has_many through relationship between user and organisation, with an org_access table joining them.

In Rails Console I type:

user = User.first
org_access = user.org_access

And it prints out:

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

But if I try:

org_access.role
user.role
user.organisation.role

None of these commands will return the role field in the org_access. How do I access a user+organisation's org_access fields?

Upvotes: 0

Views: 657

Answers (1)

user229044
user229044

Reputation: 239452

org_access is an array. A has_many association always represents 0 or more items.

You need org_access[0].role etc.

Upvotes: 3

Related Questions