Self joins result in wrong SQL #756

Closed
ssendev opened this Issue Oct 6, 2012 · 2 comments

Comments

Projects
None yet
2 participants

ssendev commented Oct 6, 2012

Given:

class User
  belongs_to :group
  has_many :group_users, through: :group, source: :users
end
class Group
  has_many :users
end
class Ability
  include CanCan::Ability
  def initialize(user)
    # can :index, User, group_users: { user_id: user.id }
    can :index, User, group: { users: { user_id: user.id }}
  end
end
User.accessible_by(current_ability)

should return the same sql as

User.joins(group: :users).where(group: { users: { user_id: user.id }})

but instead produces.

User.joins(group: :users).where(user_id: user.id)

Using the :group_users association produces the same result.

I'm having the same problem but it isn't on a self join. I think the issue may be with the way cancan is treating the source condition on the has_many.

After more digging, it seems like this is the same as: #646

ssendev closed this Nov 8, 2012

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