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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:48:46+00:00 2026-05-31T15:48:46+00:00

The plan is for my users to be able to only see other users

  • 0

The plan is for my users to be able to only see other users in users#index if those users belong to the same studio (user.studio_id).

It almost works perfectly. The only hiccup is… the first time any user (including admin) goes to users/index, they get an unauthorized message. If they try again, they get the index with only the appropriate records appearing. How can I make it stop choking on the authorization the first time they go there?

ability.rb

  class Ability
    include CanCan::Ability


      def initialize(user)
        user ||= User.new # guest user (not logged in)

         can :read, User

        if user.role? :Student
          ...
          cannot :read, User, ["studio_id <> ?", user.studio_id] do |u|
            u.studio_id != user.studio_id
          end
          can :update, User, ["id = ?", user.id] do |u|
            u.id == user.id
          end

        end

        if user.role? :Teacher
          ...
          can :update, User, ["id = ?", user.id] do |u|
            u.id == user.id
          end
        end



        if user.role? :Director
          ...
          can :manage, User, ["studio_id = ?", user.studio_id] do |u|
            u.studio_id == user.studio_id
          end
          can :create, User
        end

        cannot :delete, [Studio, Event]
        cannot :change_studio, [User]

        if user.role? :Admin
          can :manage, :all
          can :see_ids, :all
          can :change_studio, User
        end

      end


    end
  end

UsersController

    class UsersController < ApplicationController
      respond_to :html, :xml, :json
      load_and_authorize_resource #:only => [:show,:new,:destroy,:edit,:update]

      def index
      end
      ...
    end
  • 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-31T15:48:48+00:00Added an answer on May 31, 2026 at 3:48 pm

    Maybe you can try it the other way around and remove initial can :read, User and then allow by can :read, User, :studio_id => user.studio_id

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

Sidebar

Related Questions

The main user-case is: Create the 2D floor plan See the 3D view of
I have an abstract user control(baseModule) that has a property that I plan on
I plan on having an aspect of my site where the users can submit
In my Flex application, users need to be able to upload and download content.
I plan to implement a GAE app only for my own usage. The application
Im building a social networking website. A user can have pals and be able
Users of my application need to be able to schedule certain task to run
I am developing and application service and I want users to be able to
I am making a little library(DLL) to manage users and their roles/privileges. The plan
I plan to be storing all my config settings in my application's app.config section

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.