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 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 so here is the deal. As the question states, I'm trying to POST
Okay this is definitely a n00b question but here goes. The way I understand
Hope you're having a good Friday and stuff... okay, so here's my question: All
Okay, here's my short question: I know that === and !== operators will compare
Okay here's my situation. I have the following branches development development-kirby (270 commits ahead
Okay here is what I'm trying to do: I have a model object that
Okay so here's, the scenario I have a thumbnail that contains an artists profile
Okay basically here's where I'm at. I have a list of PropertyDescriptor objects. These
Okay so here is my database: I have a page on my .net form
Okay so here's the thing: I have a linq query which loads approx. 1000

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.