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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:11:29+00:00 2026-06-03T02:11:29+00:00

I have a table joining two models in a has many through relationship. Users

  • 0

I have a table joining two models in a has many through relationship.

Users have many Roles through Authorities

My Authorities table has three columns user_id role_id firm_id

I would like each authority (the join table) to have a one to one relationship with Firms. Is this possible?

— edit perhaps a little more info would make it clearer.

I have many ‘firms’

‘users’ have many firms through ‘follows’

‘users’ also have a multiple ‘roles’ through ‘authorities’

I am using the cancan gem to restrict the rights of users, so that they can follow any firm, but they can only edit the details of a single firm. This means that I cannot create a direct 1 to 1 association between Users and Firms, as they already have a has many through association – through follows. and Current_user.firms will return all the firms they are following.

As such I would like to store the firm that they have editing rights for in the Authority model, joining Users and Roles.

https://docs.google.com/drawings/d/1CiKsUEdcS6hmKa23xsapWy33NS0Gp1f11a7TgaZbAy0/edit

This should show my table layout – the dotted line is the association i would like to make.

At present my models look like this.

class Firm < ActiveRecord::Base

has_one :authority
has_many :follows, :dependent => :destroy
has_many :users, :through => :follows

class Follow < ActiveRecord::Base
belongs_to :firm
belongs_to :user

 class User < ActiveRecord::Base
 has_many :follows, :dependent => :destroy 
 has_many :firms, :through => :follows
 has_many :roles, :through => :authorities
 has_many :authorities

 class Role < ActiveRecord::Base
 has_many :users, :through => :authorities
 has_many :authorities

class Authority < ActiveRecord::Base
 belongs_to :users
belongs_to :roles
belongs_to :firm

If i can do this how would i go about selecting (in the console I shall work on from that) a specific ‘authority’ and adding a ‘firm’. Further more how would i read this nested attribute?

Thanks in Advance.

  • 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-03T02:11:31+00:00Added an answer on June 3, 2026 at 2:11 am

    I can’t see why not. What’s your problem exactly?

    class Authority < ActiveRecord::Base
        belongs_to :firm
    end
    
    class Firm < ActiveRecord::Base
        has_one :authority
    end
    

    You should be able to update the firm_id of an Authority like that:

    authority = Authority.last
    authority.firm = Firm.last # or authority.update_attribute(:firm_id, Firm.last.id)
    authority.save
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models, user and group. I also have a joining table groups_users.
I have two models, users and themes, that I'm currently joining in a HABTM
I need some help joining two table. I've got: my_type_table , which has columns:
I have two models: store and category with a joining table called categories.stores .
I have a normal joining table with two columns that are keys to other
If I have a joining table with two columns, TeamA and TeamB, how can
Ok I have two tables. Table IDAssoc has the columns bill_id , year ,
I have joining two tables,There are hundreds of records in table a and there
i have bookings table which has two people- i want to return person_1 as
I have two table. user and address . I am joining them like this

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.