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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:08:57+00:00 2026-06-13T23:08:57+00:00

Users belong to an organization. Users can view all organizations, but can only view

  • 0

Users belong to an organization. Users can view all organizations, but can only view users within their organization. When a user views the organization, I have to add filter logic to the view, so @organizations.users becomes @organizations.users.select{|u| can?(:read, u)}.

Is there a way to manage this more transparently or elegantly? Is there a way to get rid of select{|u| can?(:read, u)} or move it to a DRY place?

Relevant code:

class Organization < ActiveRecord::Base
  has_many :users
  ...
end

class Ability
  ...
  can :read, User, :organization => @user.organization
end

organizations/show.html.erb: (current, sucky version)

<% if @organization.users.select{|u| can?(:read, u)}.any? %>
  <h4>Contacts</h4>
  <ul class="unstyled">
    <% for user in @organization.users.select{|u| can?(:read, u)} %>
      <li>
        <%= link_to user, user, class: "user" %>
      </li>
    <% end %>
  </ul>
<% end %>

I want something closer to this view instead:

organizations/show.html.erb:

<% if @organization.users.any? %>
  <h4>Contacts</h4>
  <ul class="unstyled">
    <% for user in @organization.users %>
      <li>
        <%= link_to user, user, class: "user" %>
      </li>
    <% end %>
  </ul>
<% end %>

Update:

I’m avoiding accessible_by because I have an ability definition that uses a block.

class Ability
  ...
  can :read, User, :organization => @user.organization
  can :read, User do |user|
    user.is? :technical_contact
  end
end

I know that accessible_by can work with blocks, but only if a SQL fragment is provided. Since my users have many roles and are stored as a bitmask, I don’t have a SQL fragment.

  • 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-13T23:08:58+00:00Added an answer on June 13, 2026 at 11:08 pm

    CanCan has built in support for this with the accessible_by method that is available on your active record models.

    See https://github.com/ryanb/cancan/wiki/Fetching-Records for more information.

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

Sidebar

Related Questions

I have a bunch of users and each user can belong to multiple roles.
I have a resource database where resources can belong to different locations. Users and
A user can create a post. Posts have comments. A comment must belong to
In my Rails app Users can have many People which in turn can (but
In an application where users can belong to multiple groups, I'm currently storing their
I have the table users and scores . Here are the associations: belongs_to :user
I am working on a system that splits users by organization. Each user belongs
I have a database with a lot of users in it. Those users belong
I am trying to select all of the users who belong to a group.
I have a requirement to lock down access to a SharePoint library: Only users

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.