Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Using accessible_by with CanCan is not generating the correct SQL statement #954

dennismonsewicz opened this Issue Oct 24, 2013 · 0 comments


None yet
1 participant

In the app I am building, a User has the ability to add "Contributors" to their account to help them fill out certain aspects of their profile for them. I am using cancan to help facilitate defining abilities but for some reason the query that is returned for a contributor and which users they have access too is not coming back correctly.

Here is my Ability file:

module Abilities
  class UserAbility < Ability
    def initialize(user)

      if user.athlete_contributor?
        can :update, Athlete, user: { id: user.managed_athlete_ids }

User Model:

  has_many :contributorships
  has_many :managed_athletes, through: :contributorships, source: :athlete

Contributorship Model:

class Contributorship < ActiveRecord::Base
  belongs_to :user
  belongs_to :athlete
  belongs_to :contributor, :class_name => "User"
  belongs_to :user_type

  attr_accessible :user_id, :user_type_id, :athlete_id

  validates :user_type, presence: true 

Contributor Dashboard Controller:

class ContributorController < ApplicationController
  before_filter :authenticate_user!

  def index
    authorize! :update, Athlete
    @athletes = Athlete.accessible_by(current_ability)


The @athletes instance variable is returning a query of

SELECT "users".* FROM "users" WHERE "users"."type" IN ('Athlete') AND ('t'='f')

Not sure what I am doing wrong, but that query looks busted, LOL. Thanks in advance for the help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment