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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:36:59+00:00 2026-06-09T08:36:59+00:00

I currently have a setup that links the models User, Dealer and Role together.

  • 0

I currently have a setup that links the models User, Dealer and Role together. User and Dealer is many to many, and is working as expected with a Dealer_user assignment table.

The problem is that I want to have roles assigned to the user that are specific to the dealer also (i.e. a user could be a Sales Manager and a Parts Manager in one dealership, while being a Sales Manager and a Director in another).

In order to do this, I have a Role model (which belongs to a Role_type). Role should belong to Dealer_user, and Dealer_user has many Roles.

The intention is that I will be able to do

dealer.users.where(:id => user.id).first.roles

and it will return only the roles specific to that dealership.

The problem I have is that when I run the following test code:

dealer.users.where(:id => user.id).first.roles.create(:role_type_id => 1 + Random.rand(4))

I get an error:

Cannot modify association 'User#roles' because the source reflection class 'Role' is associated to 'DealerUser' via :has_many.

Can anyone suggest what I am doing wrong with my models (which are below)?

NOTE: The belongs_to relationship that Role has with Dealer_user is polymorphic because it could also belong to Sale_user or other association tables, which require the same functionality as Dealer.

class Dealer < ActiveRecord::Base
  attr_accessible :name, :address_id
  has_many :dealer_users
  has_many :users, :through => :dealer_users
  has_many :roles, :through => :dealer_users
end

class User < ActiveRecord::Base
  attr_accessible :first_name, :last_name
  has_many :dealer_users
  has_many :dealers, :through => :dealer_users
  has_many :roles, :through => :dealer_users
end

class DealerUser < ActiveRecord::Base
  attr_accessible :dealer_id, :user_id
  belongs_to :dealer
  belongs_to :user
  has_many :roles, :as => :role_originator
end

class Role < ActiveRecord::Base
  attr_accessible :role_type_id
  belongs_to :role_type
  belongs_to :role_originator, :polymorphic => true
end

Edit: No luck so far – can anyone help?

  • 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-06-09T08:37:00+00:00Added an answer on June 9, 2026 at 8:37 am

    I would use has_many through association with the roles table. Get rid of the dealer_user table, and add columns to the roles table dealer_id and user_id
    Then your models would look something like this:

    class Dealer < ActiveRecord::Base
        has_many :users, :through => :roles
        has_many :roles
    end
    
    class User < ActiveRecord::Base
        has_many :dealers, :through => :roles
        has_many :roles
    end
    
    class Role < ActiveRecord::Base
        belongs_to :dealer
        belongs_to :user
    end
    

    That should make it easier to do the types of queries you’re trying. The rails guide has a really good overview here

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

Sidebar

Related Questions

I currently have a news website setup in PHP/MYSQL that's a bit old and
I currently have a website setup where unique links are generated every time a
Currently I have a site that is set up using a masterpage and a
I currently have mirroring setup between three computers, principle, mirror, and witness. During the
I currently have replication setup on MySQL 4.1 (master, named radius) to MySQL 5.0
We currently have a single installation multi-site setup, hosted in Europe, and are looking
I have setup hadoop on top of mongodb using hadoop-mongodb driver. Currently I can
I need some advice of how to setup my tables I currently have a
Currently I have a distribution provision setup on my laptop to distribute enterprise app
currently I'm trying to have a setup where a generic database is distributed to

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.