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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:45:29+00:00 2026-06-12T05:45:29+00:00

I am a newbie to the Ruby on Rails but stuck at this interesting

  • 0

I am a newbie to the Ruby on Rails but stuck at this interesting situation :

I have three Models with me :

  1. User
  2. Company
  3. Project

I want to relate them in the following way :

  1. Company has_many Users
  2. Users belong_to Company

    Implemented via company_id column in USERS table

    User.Company or Company.Users works fine.


  1. Project has_and_belongs_to_many Users
  2. Users has_and_belongs_to_many Project

    Implemented via has_and_belongs_to_many relation using in projetcs_users table

    Project.Users or User.Projects works fine.


The real problem comes when I want them to join as following as well :

  1. Company belong_to Owners (:through => User)
  2. Owner has_one Company

    —A company can have multiple owners but one user can own just one company. He can’t be owner of multiple companies.

    —A user may or may not be owner of a company but always belong to some company.
    —A user cannot be part of two companies.

  3. Project has_one Project_manager (:through => User)

  4. Project_manager belongs_to Projects

    —A Project can have just one Project_manager but one can be project manager of multiple projects.

I am not able to figure out how the relationship should be defined in the Models. Also what should be the table structure for all three models.

My objective is to achieve :

  1. Company.Owners

    It can be done by :

    @owner = User.find(Company.owner_id)

  2. Project.Project_manager

    It can be done by :

    @Project_manager = User.find(Project.project_manager_id)

But don’t want it that way to work !!

Rest is working perfectly fine for me.

  • 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-12T05:45:30+00:00Added an answer on June 12, 2026 at 5:45 am

    This should help you get on the right track:

    class User
      # the owner relation
      #
      # user.owned_company 
      # => company
      #
      has_one :owned_company, :class_name => "Company", :foreign_key => "owner_id"
    
      # the project manager relation
      #
      # user.managed_projects 
      # => [project,...]
      #
      has_many :managed_projects, :class_name => "Project", :foreign_key => "project_manager_id"
    end
    
    class Company
      # the owner relation
      #
      # company.owner
      # => user
      #
      belongs_to :owner, :class_name => "User"
    end
    
    class Project
      # the project manager relation
      #
      # company.project_manager 
      # => user
      #
      belongs_to :project_manager, :class_name => "User"
    end
    

    Rails ActiveRecord relations are very well documented. See the documentation http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

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

Sidebar

Related Questions

I am a newbie to Ruby on Rails . I have started working on
Linking to external file in Ruby on Rails I have a file I want
I'm a newbie o thin. I have a ruby on rails application. Today I
Another newbie Ruby on Rails question: In my post view, I want to show
I'm a newbie developing my second project in Ruby on Rails: A graphic calculator.
I have the following code. I'm still a newbie in Ruby on Rails. As
I am newbie in Ruby. I installed by following this tutorial: http://udooz.net/blog/2011/02/facebook-app-using-rails-koala/ Now, when
I am facing Following problem, since i m a newbie to ruby on rails
I a newbie trying to write a [Ruby ON Rails] spec to make sure
I'm a Ruby newbie. Have a very basic question about static and instance variables.

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.