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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:03:52+00:00 2026-05-25T23:03:52+00:00

I have a class that looks something like this: class User < ActiveRecord:Base has_many

  • 0

I have a class that looks something like this:

class User < ActiveRecord:Base
  has_many :users_companies
  has_many :companies, :through => :users_companies
end

For plain users, I’d like user.companies to refer to the standard association method, but when a user is an admin, I want User.all (i.e., admins have access to all companies). The simplest way I can think of to implement this (and what I’ve always done in the past) is use a scope on the Company class, such as:

scope :accessible_by, lambda { |user| ... }

The only problem is that this just doesn’t feel right. Instead of writing a controller action that includes:

  @companies = Company.accessible_by(current_user)

I’d feel more comfortable writing

  @companies = current_user.companies

Is there a good way to override the User#companies method to accommodate this kind of behavior? Or, should I be happy with using a scope on Company?

  • 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-25T23:03:53+00:00Added an answer on May 25, 2026 at 11:03 pm

    I’m wrestling with a similar problem. The only acceptable solution I can devise is an association extension, which overrides the query for admin users and passes normal users’ queries, unmolested.

    # this works for me in rails 3.1
    class User < ActiveRecord:Base
      has_many :users_companies
      has_many :companies, :through => :users_companies do
        def visible
          if proxy_association.owner.admin?
            UsersCompany.scoped
          else
            self
          end
        end
      end
    end
    
    User.where(:admin => true).first.companies.visible == UsersCompany.all
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a rails model that looks something like this: class Recipe < ActiveRecord::Base
I have three models that look something like this: class Bucket < ActiveRecord::Base has_many
I have a model that looks something like this: class Comment < ActiveRecord::Base ...
I have a model that looks something like this public class User { public
I have a program that looks something like this: public partial class It {
I have a Hibernate mapping that looks something like this: <class name=MyEntity> <set name=scalarSet
I currently have service classes that look something like this public class UserService :
We have a class a class that looks something like the following: public class
I want to have a template class that looks something like what I have
I have something that looks like the following: [CoolnessFactor] interface IThing {} class Tester

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.