[Rails] Validating user’s ability using CanCan with model associations

Carlos A. Cabrera
2010-07-22T23:51:47Z

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!

--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at