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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:14:14+00:00 2026-05-12T17:14:14+00:00

I a little confused about how this work even if it works properly. I

  • 0

I a little confused about how this work even if it works properly. I have a model that has two association to the same other model.

Company has an owner and company has many employees of the class users.

here is my company model:

class Company < ActiveRecord::Base
  validates_presence_of :name

  has_many :employee, :class_name => 'User'
  has_one :owner, :class_name => 'User'
  accepts_nested_attributes_for :owner, :allow_destroy => true
end

here is my user model:

class User < ActiveRecord::Base
  include Clearance::User
  attr_accessible :lastname, :firstname #other attr are whitelisted in clearance gem
  validates_presence_of :lastname, :firstname
  belongs_to :company
end

Now assuming I have 3 employee of this company including the owner. When I first create the company I set the owner to the employee with id 1 and the two others (2,3) are added to the employee list by setting their company_id (user.company=company). All three have their company_id set to the company id which we can assume is 1

when I ask for company.owner, I get the right user and when I do company.employee, I get all three.

If I change the owner to user 2, it removes user 1 from the employees automatically by setting it’s company_id to nil. This is fine and if I add him back as a simple employee all is still good.

How the heck does rails know which is which? What I mean is how does it know that an employee is owner and not just an employee? Nothing in the schema defines this.

I have a feeling I should reverse the owner association and make company belong_to a user.

  • 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-12T17:14:14+00:00Added an answer on May 12, 2026 at 5:14 pm

    As you have it now, there’s nothing to distinguish owners from employees. Which means you’re going to run into problems once you start removing people or try to change ownership.

    As François points out, you’re just lucking out in that the owner is the user that belongs to company with the lowest ID.

    To fix the problem I would have my models relate in the following maner.

    class Company < ActiveRecord::Base
      belongs_to :owner, :class_name => "user"
      has_many :employees, :class_name => "user"
      validates_presence_of :name
      accepts_nested_attributes_for :owner, :allow_destroy => true
    end
    
    class User < ActiveRecord::Base
      include Clearance::User
      attr_accessible :lastname, :firstname #other attr are whitelisted in clearance gem
      validates_presence_of :lastname, :firstname
      belongs_to :company
      has_one :company, :foreign_key => :owner_id
    end
    

    You’ll have to add another column called owner_id to the Companies table, but this more clearly defines your relationships. And will avoid any troubles associated with changing the owner. Take note that there might be a cyclical dependency if you go this route and have your database set so that both users.company_id and companies.owner_id cannot be null.

    I’m not quite sure how well accepts_nested_attributes_for will play with a belongs_to relationship.

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

Sidebar

Related Questions

I am little bit confused about this statement that I read in a book
Howdy, I'm a little confused about how to work with this in jQuery (1.5.2).
I`m a little confused about this point. Everything that I found in books, blogs,
I'm a little confused about how things work in iOS development. I have a
I'm a little confused about CCSprite's position : is this it's center or bottom
I am a little confused about Accept-Encoding . I have Web Service which would
Im a little bit confused about this adapter element (NotifyDataSetChanged) cause when I use
I am a little bit confused about how javascript and the jquery lib works.
I am a little confused about when do I have to serialize objects using
I hope you forgive me if I'm a little confused about this. Jrails rewrites

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.