dennis
dennis

Reputation: 51

rails CanCan: Fetching records with accessible_by and additional 'where' clause?

I am currently using the accessible_by method of CanCan to fetch only relevant records of a model, which the logged-in user can access. So far, so good.

But how can I add to such a Foobar.accessible_by(current_ability) an additional 'where' clause?

I tried Foobar.accessible_by(current_ability).where(...), but that gives me an Exception: SQLite3::SQLException: ambiguous column name.

Upvotes: 0

Views: 873

Answers (1)

Rene van Lieshout
Rene van Lieshout

Reputation: 372

You're including a model that share the same attribute. If you want to use this attribute and keep the include you need to specify the model, e.g.:

Foo.include(:bar).where(:bar => { :name => 'xyz' })

This should be done in either your ability or in the .where(...), but that depends on the content of that ability and where.

Upvotes: 2

Related Questions