Active Record Query

I have a Projects View that shows a list of all projects… I want to
be able to say per project if the currently signed in user is a member
or not, and what there role is…

In the project View I have:
<% if team_member?(project.id, current_user) %>

Request to Join <% else %> Already Joined <% end %>

then the helper:

module ProjectsHelper
def team_member?(project_id, current_user)
if
current_user.permissions.find_by_project_id(project_id).role.try(:name).nil?
true
else
false
end
end
end

*** But that errors. Can you help me out?

Models:
permission.rb

class Permission < ActiveRecord::Base
belongs_to :user
belongs_to :project
belongs_to :role
end
project.rb

class Project < ActiveRecord::Base

has_many :permissions
has_many :users, :through => :permissions

end
role.rb

class Role < ActiveRecord::Base
has_many :permissions
end
user.rb

class User < ActiveRecord::Base

belongs_to :instance
has_many :books
has_many :permissions
has_many :projects, :through => :permissions

Error is undefined method `roles’ for nil:NilClass

I think the problem is come from this line:
current_user.permissions.find_by_project_id(project_id).role.try(:name).nil
?

And I am thinking if using try then it doesn’t need to use .nil.
You can run this query in console to debug it.

Cheers,
Lecky Lao

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs