GithubHelp home page GithubHelp logo

Comments (4)

bryanrite avatar bryanrite commented on May 20, 2024

Looks like the problem is the block section of the second ability is supposed to be run on instances of the business and the first scope is run on collections. ie:

can [:ability], Model, Model.scope_to_select_on_index_action do |model_instance|
  model_instance.condition_to_evaluate_for_new_create_edit_update_destroy
end

You should be able to do:

can :read, Business, Business.user_related(user) do |business|
   business.related_to?(user) # true/false
end

Then, when an admin tries to read a Business it will do (can read all businesses) or (can read user_related businesses)... which will always return all.

Generally I find it useful to separate out rules by roles:

case user.role
when 'admin'
  can :read, Business
  # Other admin rules
when 'user'
  # ...
else
  # guest/not logged in rules
end

from cancancan.

bryanrite avatar bryanrite commented on May 20, 2024

https://github.com/CanCanCommunity/cancancan/wiki/Defining-Abilities-with-Blocks

from cancancan.

lesliev avatar lesliev commented on May 20, 2024

Excellent, thanks a lot! Your "can [:ability]" example should be in the docs.

This still doesn't solve the problem (I still get the same error) - but when I separate out the roles like you suggest, it works. I suppose it does make sense to not run the second block at all if we have an admin.

For others, here's the working code:

    if user.admin?
      can :read, Business 
    else
      can :read, Business, Business.user_related(user) do |business|
        business.related_to?(user)
      end
    end

from cancancan.

robertokl avatar robertokl commented on May 20, 2024

+1 on the "example should be in the docs".

from cancancan.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.