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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:18:50+00:00 2026-05-14T19:18:50+00:00

I was hoping I could get feedback on major changes to how a model

  • 0

I was hoping I could get feedback on major changes to how a model works in an app that is in production already.

In my case I have a model Record, that has_many PhoneNumbers.

Currently it is a typical has_many belongs_to association with a record having many PhoneNumbers.

Of course, I now have a feature of adding temporary, user generated records and these records will have PhoneNumbers too.

I ‘could’ just add the user_record_id to the PhoneNumber model, but wouldn’t it be better for this to be a polymorphic association?

And if so, if you change how a model associates, how in the heck would I update the production database without breaking everything? >.<

Anyway, just looking for best practices in a situation like this.

Thanks!

  • 1 1 Answer
  • 1 View
  • 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-14T19:18:51+00:00Added an answer on May 14, 2026 at 7:18 pm

    There’s two approaches that might help you with this.

    One is to introduce an intermediate model which handles collections of phone numbers. This way your Record and UserRecord can both belong_to this collection model and from there phone numbers and other contact information can be associated. You end up with a relationship that looks like this:

    class Record < ActiveRecord::Base
      belongs_to :address_book
      delegate :phone_numbers, :to => :address_book
    end
    
    class UserRecord < ActiveRecord::Base
      belongs_to :address_book
      delegate :phone_numbers, :to => :address_book
    end
    
    class AddressBook < ActiveRecord::Base
      has_many :phone_numbers
    end
    

    This kind of re-working can be done with a migration and a bit of SQL to populate the columns in the address_books table based on what is already present in records.

    The alternative is to make UserRecord an STI derived type of Record so you don’t need to deal with two different tables when defining the associations.

    class Record < ActiveRecord::Base
      has_many :phone_numbers
    end
    
    class UserRecord < Record
    end
    

    Normally all you need to do is introduce a ‘type’ string column into your schema and you can use STI. If UserRecord entries are supposed to expire after a certain time, it is easy to scope their removal using something like:

    UserRecord.destroy_all([ 'created_at<=?', 7.days.ago ])
    

    Using the STI approach you will have to be careful to scope your selects so that you are retrieving only permanent or temporary records depending on what you’re intending to do. As UserRecord is derived from Record you will find they get loaded as well during default loads such as:

    @records = Record.find(:all)
    

    If this causes a problem, you can always use Record as an abstract base class and make a derived PermanentRecord class to fix this:

    class PermanentRecord < Record
    end
    

    Update during your migration using something like:

    add_column :records, :type, :string
    execute "UPDATE records SET type='PermanentRecord'"
    

    Then you can use PermanentRecord in place of Record for all your existing code and it should not retrieve UserRecord entries inadvertently.

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

Sidebar

Related Questions

I have been playing with generics and was hoping I could get some feedback
I have a design conundrum that I could use some feedback on, so I'm
I am relatively new to web development, and I was hoping I could get
I'm planning to write an interactive C++ geometry processing plug-in that will frequently sort
I have recently discovered the ability to use WHERE clauses within indexes in SQL
As hard as I try, PREG and I don't get along, so, I am
I am ready to start using SVN, but I have NO (as in the
I have read through the solutions to similar problems, but they all seem to
A few colleagues and I created a simple packet capturing application based on libpcap,

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.