I'm using CanCanCan
for authorisation in my Rails4 project, and have a set of abilities that begins:
class Ability
include CanCan::Ability
def initialize(user)
case user.role
when :administrator
can :manage, :all
when :modeller
can :manage, Model
can :manage, Scenario
#can :manage, MarketAssumption
# ... and so on.
In a view I have
<% if can? :view, Project %>
Which works exactly as it should. However, if I uncomment the line:
can :manage, MarketAssumption
the view fails on the call to can?
with "association names must be a Symbol".
MarketAssumption
is defined in app\models\market_assumption.rb
with:
class MarketAssumption < ActiveRecord::Base
belongs_to Scenario
end
in pretty much the same way as Model
and Scenario
. The controller begins:
class MarketAssumptionsController < ApplicationController
load_and_authorize_resource
before_action :set_market_assumption, only: [:show, :edit, :update, :destroy]
as I think it should.
Where else should I look for whatever makes MarketAssumption differ from Model
and Scenario
in a way that CanCanCan
doesn't like?
It's your belongs_to Scenario
line. You've given that as a class name, not a symbol. Change it to belongs_to :scenario
and your code should work fine. I expect that your can?
call is the first time Rails tried to load a MarketAssumption
object in your app, so that's the first time that error occurs.
You can find out more about setting up associations in the Rails guides.