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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:31:36+00:00 2026-05-17T21:31:36+00:00

I have a contact model, this includes name, address, phone number, etc. I have

  • 0

I have a contact model, this includes name, address, phone number, etc.

I have a user model which should have_one contact.

I have a Customer model which has_many contacts.

I have a Producer model which has many contacts.

A contact can be only a user, a user and a customer, a user and a producer, or any combination of these three. I also need to be sure that the same contact record is linked when a contact is linked to multiple models for data integrity.

how should I create the associations?

  • 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-17T21:31:37+00:00Added an answer on May 17, 2026 at 9:31 pm

    This looks like a good application for a polymorphic association:

    class User < ActiveRecord::Base
      has_one :contact, :as => :contactable
    end
    
    class Customer < ActiveRecord::Base
      has_many :contacts, :as => :contactable
    end
    
    class Producer < ActiveRecord::Base
      has_many :contacts, :as => :contactable
    end
    
    class Contact < ActiveRecord::Base
      belongs_to :contactable, :polymorphic => true
    end
    

    EDIT

    It seems I didn’t read the specs all the way through 🙂 To associate the same contact with multiple Users, Customers, etc, you could use a has_many :through:

    class User < ActiveRecord::Base
      has_one :user_contact, :dependent => :destroy
      has_one :contact, :through => :user_contact
    end
    
    class Customer < ActiveRecord::Base
      has_many :customer_contacts, :dependent => :destroy
      has_many :contacts, :through => :customer_contacts
    end
    
    class Producer < ActiveRecord::Base
      has_many :producer_contacts, :dependent => :destroy
      has_many :contacts, :through => :producer_contacts
    end
    
    class UserContact
      belongs_to :user
      belongs_to :contact
    end
    
    class CustomerContact
      belongs_to :customer
      belongs_to :contact
    end
    
    class ProducerContact
      belongs_to :producer
      belongs_to :contact
    end
    
    class Contact < ActiveRecord::Base
      has_many :user_contacts, :dependent => :destroy # might use 'has_one' here depending on your requirements
      has_many :users, :through => :user_contacts
      has_many :customer_contacts, :dependent => :destroy
      has_many :customers, :through => :customer_contacts
      has_many :producer_contacts, :dependent => :destroy
      has_many :producers, :through => :producer_contacts
    end
    

    That gives you one join table for each of the three associations. Each Contact can belong to none, one, or many of the three other models by adding rows to the join tables.

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

Sidebar

Related Questions

I have a Customer model which contains a ForeignKey to a Contact model. I
I have an Address Model which has a ForeignKey to a Contact Model: class
I have models with many to many relationships like this: class Contact(models.Model): name =
I have two models: class Contact(models.Model): name = models.CharField(max_length=255) class Campaign(models.Model): contact = models.ForeignKey(Contact,
I have a model called Contact which has_one guest like so. class Contact <
I have the following model: public class Contact { public Contact() { Name =
I have the following 4 models Hotel (name) has_one :address has_one :contact has_one :bank_account
Has anyone have contact with model checking with this tool SPIN , even more
I have this contact form which also relates to a few other tables with
Within my model I have defined a required field class like this: class Contact(models.Model):

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.