# Active Record Query

**URL:** <https://www.ruby-forum.com/t/active-record-query/195171>\
**Category:** Rails\
**Created:** [September 18, 2010, 7:06am UTC](https://www.ruby-forum.com/t/active-record-query/195171 "2010-09-18T07:06:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nobosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecb155/32.png) [@nobosh](https://www.ruby-forum.com/u/nobosh)\
**Post date:** [September 18, 2010, 7:06am UTC](https://www.ruby-forum.com/t/active-record-query/195171/1 "2010-09-18T07:06:04Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![nobosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecb155/32.png) [@nobosh](https://www.ruby-forum.com/u/nobosh)\
**Post date:** [September 18, 2010, 7:47am UTC](https://www.ruby-forum.com/t/active-record-query/195171/2 "2010-09-18T07:47:02Z")

</div>

Error is undefined method `roles’ for nil:NilClass

---

<div class="post-metadata">

**Author:** ![nobosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecb155/32.png) [@nobosh](https://www.ruby-forum.com/u/nobosh)\
**Post date:** [September 18, 2010, 4:15pm UTC](https://www.ruby-forum.com/t/active-record-query/195171/3 "2010-09-18T16:15:38Z")

</div>

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
