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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:26:55+00:00 2026-05-20T11:26:55+00:00

Okay, so here is my question. I have a 3 different models, People, Roles,

  • 0

Okay, so here is my question. I have a 3 different models, People, Roles, Client, and Store. Clients have many Stores and can also have many people. Stores have many people. People have various roles. 1 Person can work at multiple stores, and they may have different roles at each store.

For example. Joe may be an assistant manager at one store and a manager at another store. What I would like to be able to do is pull the correct roles by doing something like

Store.find(1).people.find(1).roles

(would return ‘assistant manager’ for example) or

Store.find(2).people.find(1).roles

(would return ‘manager’ for example). Is this possible to do in ActiveRecord?

I’ve created a table :roles_people which has the following definition:

create_table :roles_people, :id => false do |t|
      t.references :role
      t.references :person
      t.references :store
      t.references :client
end

However i can’t figure out how to get associations to work properly using this table. Can anyone point me in the right direction?

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-20T11:26:56+00:00Added an answer on May 20, 2026 at 11:26 am
    class People
      belongs_to :client
      has_many :store_roles
    end
    
    class Roles
      has_many :store_roles
    end
    
    class StoreRole
      belongs_to :role
      belongs_to :people
      belongs_to :store
    end
    
    class Client
      has_many :stores
      has_many :people
    end
    
    class Store
      belongs_to :client
      has_many :store_roles
      has_many :roles, :through => :store_roles
    end
    

    Assume that all of those classes inherit from ActiveRecord::Base 😉

    You’re going to need to setup the migration and database structure to mirror these relationships. For each belongs_to there is an :object_id field on the table reference the appropriate table’s id.

    Your query is going to need to look something like:

    Store.find(1).roles.find(:all, :conditions => ["store_roles.person_id = ?", 1])
    

    I would probably add a method to the store model to make this a little easier:

    def roles_for(person_id)
      roles.find(:all, :conditions => ["store_roles.person_id = ?", person_id])
    end
    

    This way you can find the roles using:

    Store.find(1).roles_for(1)
    

    Or, better yet:

    def self.roles_for(store_id, person_id)
      Role.find(:all, :joins => :store_roles, :conditions => ["store_roles.store_id = ? AND store_roles.person_id = ?", store_id, person_id])
    end
    

    Which changes our finder to:

    Store.roles_for(1, 1)
    

    I would say that this last method is the most ideal since it causes only a single query, while each of the other options execute two queries to the database per role look-up (one to find the store, and one to get the roles for a person_id). Of course if you already have the Store object instantiated then it’s not a big deal.

    Hopefully this answer was sufficient 🙂

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

Sidebar

Related Questions

Okay the question title may not have made sense... mostly because I don't know
Okay, not sure what I'm doing here, other than it's not right. Trying to
Okay. I'm sick of this problem. This has to have an easy fix, I'm
This is not a coding question, but am hoping that someone has come across
I have to select a framework (or role my own) and one of the
I am writing a numerical program that should be somewhat fast, and is also
As a best practice for front-end developers, is it bad to use the control
I've create a custom ribbon button that only shows up the Insert block for
I've been working through Bjarne Stroustrup's The C++ Programming Language (2nd edition - I
A month ago I tried to use F# agents to process and record Twitter

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.