Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1053485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:15:40+00:00 2026-05-16T17:15:40+00:00

I am using Ryan Bate’s CanCan gem to define abilities and some basic functionality

  • 0

I am using Ryan Bate’s CanCan gem to define abilities and some basic functionality is failing. I have a Product model and Products controller where my index action looks like this:

def index
    @req_host_w_port = request.host_with_port
    @products = Product.accessible_by(current_ability, :index)
end

I get an error when I try to retrieve the products with the accessible_by method, I get this error:

Cannot determine SQL conditions or joins from block for :index Product(id: integer, secret: string, context_date: datetime, expiration_date: datetime, blurb: text, created_at: datetime, updated_at: datetime, owner_id: integer, product_type_id: integer, approved_at: datetime)

My ability class looks like this:

can :index, Product do |product|
   product && !!product.approved_at
end

This seems like a very simple example so I am surprised it is failing and wondering if I am overlooking something simple (i.e. staring at my code for too long).

I did probe a bit more and ran a simple test. If you look at the code below, one example works fine and one fails where they should actually do the same exact thing.

# This works
can :index, Product, :approved_at => nil

# This fails
can :index, Product do |product|
    product && product.approved_at.nil?
end

So the problem seems to be in the way CanCan is processing these blocks. I dove deeper into the library and found where the error was raised – in CanCan’s ability class definition:

def relevant_can_definitions_for_query(action, subject)
      relevant_can_definitions(action, subject).each do |can_definition|
        if can_definition.only_block?
          raise Error, "Cannot determine SQL conditions or joins from block for #{action.inspect} #{subject.inspect}"
        end
      end
    end

So I checked out what this only_block? method is. The method returns true if a can definition has a block but no conditions which makes no sense to me because the whole point of the block is to define the conditions within the block when they are too complicated for the following syntax:

can :manage, [Account], :account_manager_id => user.id

Any insight on this issue would be great! I also filed an issue on the CanCan github page but I’m getting to the point where I may need to ditch the library. However, I understand that many people are using CanCan successfully and this is such basic functionality I think I must be doing something wrong. Especially since the git repo was updated 3 days ago and Ryan Bates mentions Rails 3 support in the README.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T17:15:40+00:00Added an answer on May 16, 2026 at 5:15 pm

    Ok so I went through the wiki and saw that accessible_by will not work when defining blocks. Seems a bit odd to have this restriction and not mention it in the README, but at least I know it’s a limitation of the library and not a bug in mine or Ryan Bates’ code. For those interested, the proper way to define my ability above is as follows:

    # will fail    
    can :index, Product do |product|
       product && product.approved_at.nil?
    end
    
    # will pass
    can :index, Product
    cannot :index, Product, :approved_at => nil
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I'm actually using Ryan' Bates cancan gem authorization to make my application's users
I'm trying to get some basic authentication/authorization with devise/cancan with Rails. Rather than using
I have a big problem when using Ryan Bates' excellent Nested_form gem. In fact,
I'm following Ryan Bate's instructions for using Omniauth to set up third party authentication,
I am using the Globalize 3 gem as seen in Ryan Bates railscasts, and
I'm using the excellent validate CFC by Ryan J. Heldt http://validation.riaforge.org/ but have a
I'm using ryan bates' plugin nested_form and i have been trying to write my
I have created a new project using devise login according to Ryan Bates' railscast
I'm using Ryan Bates nested_form gem . I'd like to be able to control
I've been following through Ryan Bates' railscast on using cancan but am stumped as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.