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 7513077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:03:55+00:00 2026-05-30T00:03:55+00:00

I am trying to get cancan to work with a polymorphic association, i have

  • 0

I am trying to get cancan to work with a polymorphic association, i have read through the docs and wiki and am not able to get it working…

I have the following models:

class User < ActiveRecord::Base
  has_many :areas, :as => :owner, :dependent => :destroy            
end 

class Account < ActiveRecord::Base  
  has_many :areas, :as => :owner, :dependent => :destroy
end

class Area < ActiveRecord::Base   
  belongs_to :owner, :polymorphic => true
end

and in the controller:

class AreasController < ApplicationController  

  load_resource :user, :instance_name => :owner
  load_resource :account, :instance_name => :owner
  load_and_authorize_resource :area, :through => :owner

  before_filter :authorize_parent

  respond_to :html

  def authorize_parent
    authorize! :manage, @owner
  end    

  def index   
  end          

  def show
    @events = @area.events.page(params[:page]).per(5)
    respond_with @area
  end

  def new 
    respond_with @area
  end               

  def create                                
    @area = @owner.areas.new(params[:area])   
    if @area.save
      flash[:notice] = "Your new area has been created..."
    end  
    respond_with @area
  end

end      

and the following abilities:

  can :manage, Area, :owner => { :memberships => { :user => { :id => user.id } } } # Accounts through Membership     
  can :manage, Area, :owner => { :id => user.id } # User

the new and create actions work great for both user_areas and account_areas but when i try to go to the index action of areas i get the following error:

NameError in AreasController#index

uninitialized constant Owner

Any thoughts? many 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-30T00:03:56+00:00Added an answer on May 30, 2026 at 12:03 am

    It considers that your class is called Owner, maybe you can try like:

    load_resource :user, :instance_name => :owner, :class=>'User'
    load_resource :account, :instance_name => :owner, :class=>'Account'
    load_and_authorize_resource :area, :through => :owner
    

    If dont works, see if this issue solves your problem: https://github.com/ryanb/cancan/issues/73

    Based on the ticket if you are using 1.3 or later version then you can do the following:

    load_resource :user
    load_resource :account
    load_and_authorize_resource :area, :through => [:user, :account]
    

    But in this case your authorize_parent method changes like this:

    def authorize_parent
      authorize! :manage, (@user||@account)
    end    
    

    For detailed documentation please see the title Polymorphic associations on : https://github.com/ryanb/cancan/wiki/Nested-Resources

    Let me know if works for you. Cheers Andy 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying get values from a GridView using the following code: foreach (GridViewRow row
Trying to get this example working from http://www.munna.shatkotha.com/blog/post/2008/10/26/Light-box-effect-with-WPF.aspx However, I can't seem to get
Trying to get comfortable with jQuery and I have encountered some sample code that
Trying to get this to work, with no luck: [DataMember] public Type ParameterType {
Trying to get a JSON output to work with jqGrid 'userdata' option. The example
I am trying get content from the link https://....com:8455 where Server's certificate is not
Hello everyone I been trying get php uploader working but having a lot of
I've been at this for 12+ exhausting hours trying get it to work on
I am using cancan and trying to get it to limit equipment shown for
I'm trying to get some basic authentication/authorization with devise/cancan with Rails. Rather than using

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.