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

  • Home
  • SEARCH
  • 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 8347947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:32:17+00:00 2026-06-09T07:32:17+00:00

Is there no-one out there who can assist with this? Having problems finding records

  • 0

Is there no-one out there who can assist with this?

Having problems finding records using cancan in my rails 3.2 app.

In my ‘nas’ index, I’m trying to display a list of ‘nas’ that belong to the current_user’s locations.

The issue seems to be that there’s no formal relationship between the user and the nas – the location owns the nas and the user owns the location.

Using the accessible_by method in my nas controller gives me unusual results. If I use the following in my ability.rb, I get an error :

 can :read, Nas, :locationusers => { :user_id => user.id }

 Error: undefined method `class_name' for nil:NilClass

And, if I change to:

 can :read, Nas, :locations => { :user_id => user.id }

I only get the nas listed for the users first location.

For example, if my user has locations with ids = 1,2,3 only the nas from location 1 are displayed.

Is there a way to display all the nas for the current user’s locations using cancan or do I have to go about this differently?

My relationships are as follows:

 class User < ActiveRecord::Base
   ...
   has_many :locationusers
   has_many :locations, :through => :locationusers
   ...
 end


class Location < ActiveRecord::Base
  ...
  has_many :locationusers
  has_many :users, :through => :locationusers

  has_many :nas
  ...
end   

class Node < ActiveRecord::Base
  ...
  belongs_to :location
end

In my ability.rb:

...
if user.role? :customer_admins
can :read, Nas, :locations => { :user_id => user.id }
..

NasController

 @nas = Nas.accessible_by(current_ability).all
  • 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-06-09T07:32:19+00:00Added an answer on June 9, 2026 at 7:32 am

    With Cancan, the User is king. If you cannot draw a line from User to the Model you are trying to authorize, it is not going to work. The easy solution is to model the relationship between Nas (the model in your example is called Node?) and User. This can be done via Locationuser (which is not shown in your example) as a has_many :through using the existing belongs_to.

    class Node < ActiveRecord::Base
      ...
      belongs_to :location
      has_many :locationusers, :through => :location
    end
    

    Now in your Cancan ability.rb, you can use:

    can :read, Node, :locationusers => { :user_id => user.id }
    

    (I changed Nas from your example to Node to correctly match the model)

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

Sidebar

Related Questions

I was trying out some exercises of db-class.org videos.There was one problem of finding
Recently I found out that there are several things that one can do to
This should be and easy one for the LINQ gurus out there. I'm doing
I hope there is someone who can help me out here. I have found
After I made some researches, I found out there is only one way to
Is there anyway to carry out an SQL transaction in one command? eg mysql_query(
One thing I've noticed with all the XML-RPC examples out there, including the spec
I'm a bit frustrated with the gems out there. It seems like each one
Just a quick one about installing a PHP website, are there any tools out
Hey guys, one more quick question for any experts out there. I have a

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.