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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:06:38+00:00 2026-06-16T20:06:38+00:00

I have created 3 models for a small system. The intent here is to

  • 0

I have created 3 models for a small system. The intent here is to handle users, companies and their relationships. I anticipate to do a lot of lookups over the intersections and also want to have strict controls over the joins available, in that a user can only belong to one company, but company can have many users. I feel the best way to represent this is with the below. I would like some feedback on this preposition. So far my tests have worked fine validating my thesis and some small implementation efforts have gone fine, but when I look through a list of users to find their company I somehow run into issues and I have a feeling it is related to the has_one relationship, but not sure. Again thanks for constructive feedback.

class User < ActiveRecord::Base
has_one  :companyrelationship, foreign_key: "user_id"
has_one  :company, :through => :companyrelationship, dependent: :destroy

class Company < ActiveRecord::Base
has_many :companyrelationships
has_many :users, :through => :companyrelationships

class CompanyRelationship < ActiveRecord::Base
belongs_to :company
belongs_to :user

validates :user_id, presence: true, uniqueness: true
validates :company_id, presence: true
  • 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-16T20:06:39+00:00Added an answer on June 16, 2026 at 8:06 pm

    I do see one thing that I’m pretty sure is an error and won’t work. I can also try to add a couple points of constructive criticism.

    The error is that the CamelCase model name is converted to underscorey (sorry, I can’t remember the term for that right now) case. All your references to companyrelationship should add an underscore. You can override the convention by setting the :class_name attribute, but I don’t think that’s what you wanted.

    Another issue I see which isn’t technically wrong, but is almost certainly not what you want is that the dependent: :destroy would delete the Company when you deleted a User. (Actually, I’m not sure dependent: :destroy works in a through: relationship.) I assume what you wanted was just to delete the CompanyRelationship. You can read the section on Dependent associations at this link:

    http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

    Next point is that the default foreign_key is the model + _id so you don’t need to specify it in this case.

    My last point is purely a style preference. I like doing my validations by validation type. You’re doing them by variable. They are functionally equivalent, so feel to take it with a grain of salt. (I’m pretty sure user and user_id are equivalent, but feel free to stick to user_id if you’re more comfortable with it.)

    If it were completely up to me, I would code it this way:

    class User < ActiveRecord::Base
      has_one  :company_relationship, dependent: :destroy
      has_one  :company, through: :company_relationship
    
    class Company < ActiveRecord::Base
      has_many :company_relationships
      has_many :users, through: :company_relationships
    
    class CompanyRelationship < ActiveRecord::Base
      belongs_to :company
      belongs_to :user
    
      validates_presence_of :user, :company
      validates_uniqueness_of :user
    

    I hope that helps you get further.

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

Sidebar

Related Questions

NoSQL databases & particularly Cassandra have created a lot of buzz with their high
I have created a play application and have some models. One of them contains
Hi So I have created a quick table: package models; import javax.persistence.*; import play.db.ebean.Model;
I have a model with a datetime field: class MyModel(models.Model): created = models.DateTimeField(auto_now =
I'm developing a booking system in Rails 3.1. I have created a model for
I have created a use case of a small application and now I have
I have two tables big (id, bigs_name, smallid), small(id, smallguys_name) therefore two models -
I have created a login system and I am trying to stick to the
I have created a small SIFT application that grabs the keypoints and saves it
I have always wanted to make a small easy to use CMS system just

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.