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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:03:27+00:00 2026-06-09T05:03:27+00:00

I am currently trying to create a fairly simple association in Rails, but it

  • 0

I am currently trying to create a fairly simple association in Rails, but it is proving more difficult than I thought.

I have 3 models, User, Dealer and Role. User/dealer is many to many. Users can have many roles, but they should be specific to a dealership – i.e. a user can have the roles manager and director at one dealership, manager only at another, and be chairman in another.

The outcome I am looking for is that I will be able to do user.dealer.roles and (e.g. for the first dealer) it would return manager and director. The same would happen if I did dealer.user.roles.

My classes look as follows:

class User  :dealer_users
  has_many :dealer_user_roles, :through => :dealer_users
  has_many :roles, :through => :dealer_user_roles
end

class Dealer  :dealer_users
  has_many :dealer_user_roles, :through => :dealer_users
  has_many :roles, :through => :dealer_user_roles
end

class Role  :dealer_user_roles
  has_many :users, :through => :dealer_users
end

The tables look like (green tables):

Model diagram

I am trying to seed the database using the following code:

dealer = Dealer.where(name: Faker::Company.name).first_or_create
user = User.where(email: Faker::Internet.email).first_or_create
user.first_name = Faker::Name.first_name 
user.last_name = Faker::Name.last_name
user.password = 'password'
user.password_confirmation = 'password'
dealer.users  user.id).first.roles  1 + Random.rand(4))

This results in an error, which is: "Cannot modify association 'User#roles' because it goes through more than one other association."

Can anyone assist by telling me where I am going wrong, and how I can put it right?

  • 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-09T05:03:29+00:00Added an answer on June 9, 2026 at 5:03 am

    Off the top of my head, I would model it as:

    # Table name: users
    #  id          :integer  not null, primary key
    #  name        :string
    class User
      has_many :employments
      has_many :dealers, :through => :employments
      has_many :roles, :through => :employments
    end
    
    # Table name: dealers
    #  id          :integer  not null, primary key
    #  name        :string
    class Dealer
      has_many :employments
      has_many :users, :through => :employments
      has_many :roles, :through => :employments
    end
    
    # Table name: employments
    #  id          :integer  not null, primary key
    #  user_id     :integer  not null
    #  dealer_id   :integer  not null
    class Employment
     belongs_to :dealer, :inverse_of => :employments
     belongs_to :user,   :inverse_of => :employments
     has_many :roles
     scope :for_user,   lambda{ |user| where(:user_id => user.id) }
     scope :for_dealer, lambda{ |dealer| where(:dealer_id => dealer.id) }
    end
    
    # Table name: roles
    #  id             :integer   not null, primary key
    #  employment_id  :integer   not null
    #  name           :string    not null
    class Role
      belongs_to :employment
      scope :at_dealership, lambda{ |dealer| joins(:employment).where(:dealer_id => dealer.id) }
      scope :for_employee,  lambda{ |employee| joins(:employment).where(:user_id => employee.id) }
      # alternately:
      # scope :at_dealership, lambda{ |dealer| joins(:employment).where('employments.dealer_id = ?', dealer.id) }
      # scope :for_employee,  lambda{ |employee| joins(:employment).where('employments.user_id = ?',employee.id) }
    end
    

    This makes it easier to deal with, and also puts the roles where they actually belong – on the combination of user/dealer which I’ve called employment.

    you can then say

    dealer.users.first.roles 
    # or
    user.dealers.where('some condition').roles
    

    EDIT: with scopes all of these should do what you want.

    user = User.first
    dealer = user.dealers.first
    
    user.roles.at_dealership(dealer)
    
    dealer.roles.for_employee(user)
    
    dealer.employments.for_user(user).roles
    
    user.employments.for_dealer(dealer).roles
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to create a fairly simple application in WPF following the
I'm currently trying to create an email application using Rails and Sendgrid. This application
I am trying to create a simple 3-D app for android that will have
I'm currently trying to create a ray tracer in C++ but I'm having difficulty
I am currently trying to create a very simple thread pool using std::thread .
I'm trying to create a fairly simple trigger that would add one to a
I'm currently trying to create a template in TextMate. The template is very basic:
I'm currently trying to create a program that estimates location based on signal strength.
I am currently trying to create a function which will allow me to pass
I'm currently trying to create a desktop like homepage where using can move around

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.