Imagine a has_many relationship for memberships for clubs:
end
class Club < ActiveRecord::Base
has_many :memberships, :dependent => :destroy
has_many :users, :through => :memberships
validates :name, :is_enrollable, :presence => true
end
class Membership < ActiveRecord::Base
belongs_to :club
end
Assume also that Club has a boolean is_enrollable
field in its table. When true, a user can create a Membership associated with that Club. When false, only an admin may create the Membership record.
My question is: how do you set up CanCan's ability.rb to reflect this?
Comment: It's slightly unusual in that a field in the Club table controls the ability to create a Membership record. This cannot work:
can :create, Membership, :club => {:is_enrollable => true}
... since the Membership doesn't exist before its created. Edit: that's not true -- CanCan will work on an unsaved record before authorizing it. See answer below.