WozPoz
WozPoz

Reputation: 992

ActiveRecord::Relation error

Error from Rails, does this make sense to you?

<%= @permission.inspect %> outputs: [#<Permission project_id: 3, role_id: 2, user_id: 13>]

<%= Role.find(@permission.role_id) %>

undefined method `role_id' for [#<Permission project_id: 3, role_id: 2, user_id: 13>]:ActiveRecord::Relation

This doesn't work either for some reason: @permission.role.name

Any Ideas? thanks

Upvotes: 0

Views: 1833

Answers (2)

shingara
shingara

Reputation: 46914

@permission is an Array so you need iterate on it

<% @permission.each do |perm| %>
  <%= Role.find(perm.role_id) %>
<% end %>

If you want only one @permission return :

@permission = Permission.where(["user_id = ? AND project_id = ?", @user.id, @project.id]).first

Upvotes: 2

Swanand
Swanand

Reputation: 12426

@permissions is a collection (Array). Try @permissions.first.role_id

Upvotes: 3

Related Questions