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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:20:06+00:00 2026-05-27T13:20:06+00:00

so first my model structure: company has many users, user belongs to company model1

  • 0

so first my model structure:

company has many users, user belongs to company

model1 belongs to company, company has many model1

model2 belongs to company, company has many model2

for the index action of model1 i simply do something like

@model1 = current_user.company.model1s # i use devise for auth

in my controller i put first

before_filter :authenticate_user!

the problem is, the show action (of course) shows every logged in user every record of model one.

what is the best option to restrict model1 and model2 records only to users that belong to the company the model1 and model2 are belonging to?

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-27T13:20:06+00:00Added an answer on May 27, 2026 at 1:20 pm

    In the general case, use a plugin or gem like (in no particular order) CanCan, Clearance or one of the others here. For what it’s worth, I’ve used acl9 successfully in the past.

    Most of these solutions implement authorization control at the controller/view level, and control access to specific instances of models (AKA rows in the database) that way.

    Also see this question, which covers the topic from the perspective of which is best.

    Finally, if you want to roll your own, the simplest solution is to join from model1 and model2 to company and user in your queries. From your description, model1 and model2 have a company_id column, and company has a user_id column. Depending on how you have the associations in your models set up, you could do something like:

    Model1.joins(:company => :user).where(:users => {:id => 1})
    

    or (assuming an appropriate authentication framework):

    Model1.joins(:company => :user).where(:users => {:id => current_user.id})
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my model structure: A User has and belongs to many groups A
I have a simple one-to-many structure like this: class User(db.Model): userEmail = db.StringProperty() class
In model first, when we create a one to many relationship between two entities,
So I have the following model structure for a django project class RawData(models.Model): user
First Model class A(db.Model): number = db.IntgerProperty() second Model class B(db.Model): name = db.StringProperty()
I've two models: The first model: class Url < ActiveRecord::Base attr_accessible :code, :target, :user_id,
I have an entity defined as in a db First Model: public class Merlin_BR_Condiciones_Item
How can I search first in model (i know how to do this). And
I am using MVC-View Model, EF Model first I am having problems with two
I am trying to do this with MVC 3 EF Model first I want

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.