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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:47:07+00:00 2026-05-17T19:47:07+00:00

A User can have many roles, but only one role per Brand. Class User

  • 0

A User can have many roles, but only one role per Brand.

Class User < AR::Base
    has_and_belongs_to_many :roles, :join_table => "user_brand_roles"
    has_and_belongs_to_many :brands, :join_table => "user_brand_roles"
end

The problem with this setup is, how do I check the brand and the role at the same time?

Or would I better off with a BrandRole model where different roles can be set up for each Brand, and then be able to assign a user to a BrandRole?

Class User < AR::Base
has_many :user_brand_roles
has_many :brand_roles, :through => :user_brand_roles
end

Class BrandRole < AR::Base
belongs_to :brand
belongs_to :role
end

Class UserBrandRole < AR::Base
belongs_to :brand_role
belongs_to :user
end

This way I could do a find on the brand for the user:

br = current_user.brand_roles.where(:brand_id => @brand.id).includes(:brand_role)
if br.blank? or br.role != ADMIN
  # reject access, redirect
end

This is a new application and I’m trying to learn from past mistakes and stick to the Rails Way. Am I making any bad assumptions or design decisions here?

  • 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-17T19:47:08+00:00Added an answer on May 17, 2026 at 7:47 pm

    Assuming Roles,Brands are reference tables. You can have a single association table Responsibilities with columns user_id, role_id, brand_id.
    Then you can define

    Class User < AR::Base
     has_many : responsibilities
     has_many :roles, :through => responsibilities
     has_many :brands,:through => responsibilities
    end
    Class Responsibility < AR::Base
     belongs_to :user
     has_one :role
     has_one :brand
    end
    

    The you can define

    Class User < AR::Base
      def has_access?(brand)
         responsibility = responsibilities.where(:brand => brand)
         responsibility and responsibility.role == ADMIN
      end
    end
    

    [Not sure if Responsibility is the term used in your domain, but use a domain term instead of calling it as user_brand_role]

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

Sidebar

Related Questions

I have a table containing roles. Many users can be assigned to a role.
I have the following models to associate users with roles: class User < ActiveRecord::Base
I have a page upon which a user can choose up to many different
I have a table of users profiles. Every user can have many profiles and
Well i need to make it so any user can have a foo.com/username in
I have a web application with users and their documents. Each user can have
I have a form a user can enter their name, then it will add
I have a form in which the user can choose a component type from
I want to have a text box that the user can type in that
We have a page of search results which the user can hit in several

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.