Validating user’s ability using CanCan wit h model associations

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!

On Thu, Jul 22, 2010 at 19:51, Carlos A. Cabrera [email protected]
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
-Robert Anson Heinlein | Programming: http://codosaur.us
-------------------------------+ Leadership: http://dare2xl.com
Have Pun, Will Babble! -me | Et Cetera: http://davearonson.net

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