0

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 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

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
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.
2

@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

4 Comments

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
use @permission = Permission.where(["user_id = ? AND project_id = ?", @user.id, @project.id]).first
Thanks... If there is only 1 matching result in the DB, will it return an array? Or is it always an array?
it's return nil or your object no anymore Array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.