Writing a Rails cancan ability using an association, that works with accessible_by -
Writing a Rails cancan ability using an association, that works with accessible_by -
working on app user
belongs_to organization
. organization
has_and_belongs_to_many products, though organizations_products
table.
i want user
particular role able manage products organization
. in ability.rb
:
def initialize(user) # ...snip unrelated stuff elsif user.is_manager? can :manage, product, |product| user.organization.products.include?(product) end
this describes want raises exception in products controller:
def index @products = product.accessible_by(current_ability) end
because acessible_by
can't used blocks in ability definitions. how can write ability in way compatible accessible_by
?
can :manage, product, user.organization.products |product| user.organization.products.include?(product) end
to clarify: cancan added 3rd parameter, can understand scope used accessible_by, in version 1.6. block used evaluating can :manage, @a_specific_product
, , if utilize either of these—the scope or block—you should utilize both.
see also: this question, dup of one, , the docs.
ruby-on-rails-3 cancan
Comments
Post a Comment