title: Using CanCan to authorize a resource based on a many to many association
body: <p>I have two Models, Events and Users that share a many to many association. A user could be a Admin, Manager or a Producer.
  Only a producer that belongs to a event should be able to read that event. I tried to apply that restriction on the ability model but it's fails and every producer can read all events. What am I doing wrong?</p>
  
  <pre><code>class Event &lt; ActiveRecord::Base
   has_and_belongs_to_many :producers, :class_name =&gt; "User", :join_table =&gt; "events_producers"
  end
  
  
  class CreateEventUserJoinTable &lt; ActiveRecord::Migration
    def self.up
      create_table :events_producers, :id =&gt; false do |t|
        t.integer :event_id
        t.integer :user_id
      end
    end
  
    def self.down
      drop_table :events_producers
    end
  end
  
  class Ability
    include CanCan::Ability
    def initialize(user)
      user ||= User.new() # Guest user
      if user.role? :manager
        can :manage, :all
      elsif user.role? :admin
        can :read, Event
        can :update, Event
        can :create, Event
      elsif user.role? :producer
        can :read, Event do |event|
            event.try(:producers).include?(user)
          end
      end
    end
  end
  </code></pre>