Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Specify cancan attributes_for conditions even when using block syntax
# A quick hack that shows how to add a hash
# of conditions to cancan rules when the block syntax is used
# (sadly, you can't use the conditions and the block syntax at the time, so
# when you need to use a block you have to give up the conditions. until now!)
class Ability
include CanCan::Ability
def initialize(user)
# some rules here ...
can :create, Pizza do |pizza|"pizza") ||
rules.last.instance_variable_set("@conditions", { user_id: }) when PizzaController#new builds a Pizza via load_resource
#that pizza's user_id will automatically be populated
#just as if you had written the rule as:
# can :create, Pizza, user_id:
# more rules here...

This comment has been minimized.

Show comment Hide comment

loveybot May 26, 2012

Yay Ruby!!

Yay Ruby!!

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