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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:57:33+00:00 2026-05-22T23:57:33+00:00

I have a user model, farmer model, doctor model, and education model. A farmer

  • 0

I have a user model, farmer model, doctor model, and education model.

A farmer has a user and many educations.

A doctor has a user and many educations.

How do I setup the database for the education model?

Should it have a farmer_id AND a doctor_id?

But a education cannot belong to a farmer AND and doctor at the same time. It’s one or the other.

So my education database entry would either have a farmer_id OR a doctor_id filled in, but not both.

Is there a way to guarantee that only one of the ids could be filled in at a time?

Or is there a better way to associate these models?

Your help would be appreciated!

Oh, and don’t worry about the names of the models (farmer, doctor, etc.). It’s just an example.

  • 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-22T23:57:34+00:00Added an answer on May 22, 2026 at 11:57 pm

    I see two possible solutions for this scenario.

    The first one is to make use of polymorphic associations for education. That could look like this:

    class Farmer < ActiveRecord::Base
      belongs_to :user
      has_many :educations, :as => :profession
    end
    
    class Doctor < ActiveRecord::Base
      belongs_to :user
      has_many :educations, :as => :profession
    end
    
    class Education < ActiveRecord::Base
      belongs_to :profession, :polymorphic => true
    end
    

    So instead of education having a doctor_id or a farmer_id it has one profession_id and one profession_type.

    The second solution would be to make use of Single Table Inheritance. And in your scenrio, that could be accomplished by letting a Doctor be a User instead of belonging to a User. And of course the same thing for a Farmer. That could look like this:

    class User < ActiveRecord::Base
      has_many :educations
    end
    
    class Farmer < User
    end
    
    class Doctor < User
    end
    
    class Education < ActiveRecord::Base
      belongs_to :user
    end
    

    And in this scenario you would add a type column to the User model to store what type of class it is and then only having a user_id in the Education model

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

Sidebar

Related Questions

I have a User model that has many fights. Fight belongs to User. There
I have a User model which has many projects and a Project model which
I have a User model and an Account model. The user has many accounts
I have a User model, Person model and Company model. a User has many
I have a User model and a Submission model. Each Submission has a ForeignKey
I have User model and Group model. User belongs_to Group . Group has an
I have a user model that has properties such as username, password, first name,
i have a user model which has one image. how can i disable the
I have a User model that has a Roles collection. The Roles collection has
I have a user model which has_one profile. The user model has a name

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.