Skip to content

Ability generating strange SQL #830

Open
rvelasquez opened this Issue Feb 28, 2013 · 5 comments

3 participants

@rvelasquez

Hi,

I have an ability defined like so:

can :manage, AssignedTask, :task => {:job => {:franchise_id => user.franchise_id}}

My models define that the AssignedTask belongs to a Task which belongs to a Job which belongs to a Franchise.

When I use AssignedTask.accessible_by(ability) I get the error:

ActiveRecord::StatementInvalid: PG::Error: ERROR: column tasks.jobs does not exist
LINE 1: ...IN "jobs" ON "jobs"."id" = "tasks"."job_id" WHERE "tasks"."j...
^
: SELECT "assigned_tasks".* FROM "assigned_tasks" INNER JOIN "tasks" ON "tasks"."id" = "assigned_tasks"."task_id" INNER JOIN "jobs" ON "jobs"."id" = "tasks"."job_id" WHERE "tasks"."jobs" = '---
:franchise_id: 1
'

The issue is the final WHERE clause which shouldn't be there and I have no idea where it's coming from. AssignedTask.accessible_by(ability).to_sql returns:

"SELECT \"assigned_tasks\".* FROM \"assigned_tasks\" INNER JOIN \"tasks\" ON \"tasks\".\"id\" = \"assigned_tasks\".\"task_id\" INNER JOIN \"jobs\" ON \"jobs\".\"id\" = \"tasks\".\"job_id\" WHERE \"tasks\".\"jobs\" = '---\n:franchise_id: 1\n'"

Any ideas?

@rvelasquez

Also this problem only started happening when I tried to upgrade from Rails 3.2.2 to 3.2.12.

@rvelasquez

Spent some time tracking this down. Looks like it's a bug in the current release of Rails 3.2.2. More info here: rails/rails#9511

@DanOlson

I had this problem as well. I tracked it down and submitted this pull request.
rails/rails#9859

@DanOlson

This issue seems to be fixed in master. ActiveRecordAdapter#tableized_conditions will now return something that ActiveRecord::PredicateBuilder can deal with. I guess, until the next version of the gem is released, it's still going to be an issue for users.

@xhoy
xhoy commented Jul 1, 2014

Thanks for your submission! The ryanb/cancan repository has been inactive since Sep 06, 2013.
Since only Ryan himself has commit permissions, the CanCan project is on a standstill.

CanCan has many open issues, including missing support for Rails 4. To keep CanCan alive, an active fork exists at cancancommunity/cancancan. The new gem is cancancan. More info is available at #994.

If your pull request or issue is still applicable, it would be really appreciated if you resubmit it to CanCanCan.

We hope to see you on the other side!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.