views:

32

answers:

2

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

+2  A: 

@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
shingara
Thanks. but permission should never return more than one record. Is there a way to obtain the var in Rails to specifiy it's not a collection but a single record? Right now I have: @permission = Permission.where(["user_id = ? AND project_id = ?", @user.id, @project.id]) thanks
WozPoz
use @permission = Permission.where(["user_id = ? AND project_id = ?", @user.id, @project.id]).first
shingara
Thanks... If there is only 1 matching result in the DB, will it return an array? Or is it always an array?
WozPoz
it's return nil or your object no anymore Array
shingara
+3  A: 

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

Swanand
Thanks. but permission should never return more than one record. Is there a way to obtain the var in Rails to specifiy it's not a collection but a single record? Right now I have: @permission = Permission.where(["user_id = ? AND project_id = ?", @user.id, @project.id]) thanks
WozPoz
Yes, in that case, as Shingara has said, `@permission = Permission.where(["user_id = ? AND project_id = ?", @user.id, @project.id]).first`. This will either return the permission object, or nil if no matching rows are found.
Swanand