# Validating userâ€™s ability using CanCan wit h model associations

**URL:** <https://www.ruby-forum.com/t/validating-usera-s-ability-using-cancan-wit-h-model-associations/192053>\
**Category:** Rails\
**Created:** [July 23, 2010, 1:53am UTC](https://www.ruby-forum.com/t/validating-usera-s-ability-using-cancan-wit-h-model-associations/192053 "2010-07-23T01:53:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![corneto82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb9ed0/32.png) [@corneto82](https://www.ruby-forum.com/u/corneto82)\
**Post date:** [July 23, 2010, 1:53am UTC](https://www.ruby-forum.com/t/validating-usera-s-ability-using-cancan-wit-h-model-associations/192053/1 "2010-07-23T01:53:17Z")

</div>

I have a TakeAction model that looks like this:

class TakeAction \< ActiveRecord::Base  
belongs\_to :user  
has\_many :take\_action\_notes  
attr\_protected :user\_id  
end  
and a TakeActionNote model that looks like this:

class TakeActionNote \< ActiveRecord::Base  
belongs\_to :take\_action  
validates\_presence\_of :note  
end  
Using CanCan, I’m trying to allow the user that owns the take\_action  
to create and destroy (manage) notes.

I’ve tried defining abilities with a block like this.

can :manage, TakeActionNote do |action, note|  
note.take\_action.user.id == user.id  
end  
I can add notes to the take\_action without any problem but when I try  
to destroy it I get:

NoMethodError in Take action notesController#33 undefined method  
`take\_action’ for nil:NilClass

Is there anything I’m missing or doing wrong? Any help is appreciated.  
Thanks!

---

<div class="post-metadata">

**Author:** ![corneto82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb9ed0/32.png) [@corneto82](https://www.ruby-forum.com/u/corneto82)\
**Post date:** [July 23, 2010, 2:13pm UTC](https://www.ruby-forum.com/t/validating-usera-s-ability-using-cancan-wit-h-model-associations/192053/2 "2010-07-23T14:13:42Z")

</div>

On Thu, Jul 22, 2010 at 19:51, Carlos A. Cabrera [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> can :manage, TakeActionNote do |action, note|  
> note.take\_action.user.id == user.id  
> end  
> I can add notes to the take\_action without any problem but when I try  
> to destroy it I get:
> 
> NoMethodError in Take action notesController#33 undefined method  
> `take\_action’ for nil:NilClass

Undefined methods on nil:NilClass mean that you’re expecting something  
to be an object, but it’s really nil. With something this short, it’s  
probably not so much _this_ code, as whatever call it (or causes it to  
be called, whatever). If you don’t know what calls it, perhaps you  
can insert something that would look for a nil value and dump the call  
stack in that case. (Sorry I can’t be more specific, as I don’t know  
CanCan myself; this is just a general code troubleshooting technique.)

-Dave

–  
Specialization is for insects. | Professional: [http://davearonson.com](http://davearonson.com)  
-Robert Anson Heinlein | Programming: [http://codosaur.us](http://codosaur.us)  
-------------------------------+ Leadership: [http://dare2xl.com](http://dare2xl.com)  
Have Pun, Will Babble! -me | Et Cetera: [http://davearonson.net](http://davearonson.net)
