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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:48:43+00:00 2026-05-22T16:48:43+00:00

I apologize if the question title is confusing. I have the following situation: I

  • 0

I apologize if the question title is confusing. I have the following situation: I have a Person model which stores people in the standard way in the people table.

I need to add a has_many relationship for emergency_contacts on the Person model. I tried doing this in the following way:

Migrations:

create_table :people do |t|
  t.string :first
  t.string :middle
  t.string :last

  t.timestamps
end
create_table :emergency_contacts, :id => false do |t|
  t.integer :person_id
  t.integer :emergency_contact_id

  t.timestamps
end

Models:

class Person < ActiveRecord::Base
  has_many :emergency_contacts

  validates :first, :presence => true
  validates :last, :presence => true
end
class EmergencyContact < ActiveRecord::Base
  belongs_to :person
  has_one :person, :foreign_key => 'emergency_contact_id'
end

This allows me to do:

ruby-1.9.2-p180 :001 > p = Person.new(first: "John", last: "Doe")
 => #<Person id: nil, first: "John", middle: nil, last: "Doe", created_at: nil, updated_at: nil> 
ruby-1.9.2-p180 :002 > ec = EmergencyContact.new
 => #<EmergencyContact person_id: nil, emergency_contact_id: nil, created_at: nil, updated_at: nil> 
ruby-1.9.2-p180 :003 > ec.emergency_contact = Person.new(first: "Peter", last: "Griffin")
 => #<Person id: nil, first: "Peter", middle: nil, last: "Griffin", created_at: nil, updated_at: nil> 
ruby-1.9.2-p180 :004 > p.emergency_contacts << ec
 => [#<EmergencyContact person_id: nil, emergency_contact_id: nil, created_at: nil, updated_at: nil>] 
ruby-1.9.2-p180 :005 > p.save!
 => true 

However, I don’t feel the EmergencyContact model should have to be there, since I am really just referencing a Person model anyway.

Is there a way to remove this “middle-man” model, so that I can just do something like:

p = Person.new(first: "John", last: "Doe")
p.emergency_contacts << Person.new(first: "Peter", last: "Griffin")
  • 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-22T16:48:44+00:00Added an answer on May 22, 2026 at 4:48 pm

    I’d really use self referential associations for this kind of purpose.

    Se tutorial here: http://railscasts.com/episodes/163-self-referential-association

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

Sidebar

Related Questions

I apologize if the question already exists... Is there a way to have a
In advance, I apologize if the question title may not be be convenient. I'm
First, I wish to apologize if the title is vague, the question is hard
Apologies for the poor question title. I have two tables, jobs and Persons-Jobs. Jobs
I apologize for the title.. i couldn't find a better way to explain the
First I'll apologize for the unclear title of my question. I wasn't sure how
firstly, I very much apologize for my poorly written title of this question. So
This could be a repeat question, I apologize. I have a jsp page in
I apologize for vague title so let me explain. Suppose you have a page
I apologize for the title, but I couldn't think of a better way to

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.