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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:25:02+00:00 2026-05-15T14:25:02+00:00

I have stumbled on an interesting challenge regarding active record associations: I have an

  • 0

I have stumbled on an interesting challenge regarding active record associations:

I have an Account model which can have multiple and different organisations attached to it (like for example a Company, a Contractor, a Person) and it also has a different role with each association (accountant, owner, viewer, etc.).

So I am not sure whats the best way to associate them.

  • 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-15T14:25:03+00:00Added an answer on May 15, 2026 at 2:25 pm

    I’m taking Elimantas’ example, corrected some logic errors. basically, you have an Account with N Organizations (which is a polymorphic model and has relation with one of Company, Contractor, etc…)

    class Account < ActiveRecord::Base
      has_many :organizations
    end
    
    class Organization < ActiveRecord::Base
      belongs_to :account
      belongs_to :resource, :polymorphic => true
    end
    
    class Company < ActiveRecord::Base
      has_many :organizations, :as => :resource
    end
    
    class Contractor < ActiveRecord::Base
      has_many :organizations, :as => :resource
    end
    
    [...etc...]
    

    EDIT:
    here’s the same approach to manage Roles:

    # just edited Account model with role.
    class Account < ActiveRecord::Base
      has_many :organizations
      has_one :role
    end
    
    class Role < ActiveRecord::Base
      belongs_to :account
      belongs_to :resource, :polymorphic => true
    end
    
    class Accountant < ActiveRecord::Base
      has_one :role, :as => :resource
    end
    
    class Owner < ActiveRecord::Base
      has_one :role, :as => :resource
    end
    

    EDIT2:

    thirth edit, now the relation is: Account has_many Organizations, each Organization has_one Role, an Account has_many Roles through Organizations

    class Account < ActiveRecord::Base
      has_many :organizations
      has_many :roles, :through => :organizations
    end
    
    class Role < ActiveRecord::Base
      belongs_to :resource, :polymorphic => true
    end
    
    class Accountant < ActiveRecord::Base
      has_one :role, :as => :resource
    end
    
    class Owner < ActiveRecord::Base
      has_one :role, :as => :resource
    end
    

    is this right?

    EDIT3: Role as non AR model:

    It may depend by how many Roles you plan to have, or you may have a name:string field where specify ‘Accountant’, ‘Owner’, and so on.

    if you don’t plan to put Role as AR model, then you can define an integer column called ‘role_id’ and use a costant with an hash in the organization model:

    class Organization < ActiveRecord::Base
       belongs_to :account
    
       ROLES={'Accountant' => 1, 'Owner' => 2 }
    
       # get role as literal name
       def role
          ROLES.invert[role_id]
       end
    end
    

    this way you’ll have an Account with many Organizations, each of them with a specific Role regarding that specific Account.

    EDIT4: code example of EDIT3
    the following code is a raw example of how it might look this association:

    # new account
    a = Account.new(....)
    # new company & organization with a role
    comp = Company.create(....)
    org1 = Organization.new(:role_id => 1, ....)
    org1.resource = comp
    
    # new Contractor & Organization with another role
    contr = Contractor.create(....)
    org2 = Organization.new(:role_id => 2, ....)
    org2.resource = contr
    
    a.organizations << org1
    a.organizations << org2
    
    # save Account, it will have 2 different organizations, each one with a specific role
    a.save
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm quite new to hibernate and have stumbled on this problem, which I can't
I have stumbled upon a riddle which I can't explain, maybe someone here will
I'm pretty new in iphone programming and have stumbled upon this issue which I
While looking for something totally different the other day, I have stumbled upon two
I have stumbled upon a problem when calling a nested Async which happens to
I am currently experimenting with various input controls and have stumbled upon SneakyInput, which
I seem to have stumbled into an interesting anomoly with KnockoutJS. I have an
I have stumbled across this insane behavior where image.onload gets fired multiple times instead
I seem to have stumbled over a problem regarding saving an xml file from
Today I stumbled upon an interesting bug I wrote. I have a set of

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.