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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:44:10+00:00 2026-05-14T05:44:10+00:00

I created a rails model by doing script/generate model Customer name:string address:string city:string state:string

  • 0

I created a rails model by doing

script/generate model Customer name:string address:string city:string state:string zip:integer [...]

I filled the database with 5000 customers and started building my app. Now I’ve realized my model isn’t normalized: I often have multiple customers at the same address! If I wish to do something per-address, like, say, a mailing, this causes problems. What I’d like to have is a Address model, a Customer model, and a Mailing model.

Is there a rails way to normalize an existing model, splitting it into two models? Or should I just write a script to normalize my existing data, then generate new models accordingly?

  • 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-14T05:44:10+00:00Added an answer on May 14, 2026 at 5:44 am

    You asked about what the migration would look like. Rather than cram this in a comment reply, I created a new answer for you.

    script/generate model address customer_id:integer address:string city:string state:string zip:integer

    class CreateAddresses < ActiveRecord::Migration
      def self.up
        create_table :addresses do |t|
          t.integer :customer_id
          t.string :address
          t.string :city
          t.string :state
          t.integer :zip_code
          t.timestamps
        end
    
        # move customer address fields to address table
        Customer.all.each do |c|
          Address.create({
            :customer_id => c.id,
            :address => c.address,
            :city => c.city,
            :state => c.state,
            :zip => c.zip
          })
        end
    
        # you'll have to write your own merge script here
        # use execute("your sql goes here...") if you can't do it with ActiveRecord methods
    
        # remove old customer address columns
        remove_columns(:customers, :address, :city, :state, :zip)
    
      end
    
      def self.down
    
        # here you will define the reverse of the self.up method
    
        # re-add the address columns to the user table
    
        # repopulate the customer table with data from the address table
    
        drop_table :addresses
      end
    end
    

    Resources

    1. AcitveRecord::Migration
    2. execute
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 357k
  • Answers 357k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You probably want to call setlocale() first, "LC_ALL" should do… May 14, 2026 at 9:06 am
  • Editorial Team
    Editorial Team added an answer Linux Ubuntu Desktop Jaunty Firebug FireCookie Pixel Perfect Web developer… May 14, 2026 at 9:06 am
  • Editorial Team
    Editorial Team added an answer Your code should look like this: var par = [];… May 14, 2026 at 9:06 am

Related Questions

I'm building an app in Ruby on Rails, and I'm including 3 of my
I have a rails time-based query which has some odd timezone sensitive behaviour, even
One of the things I'm doing includes several links on the show view. For
New to rails and trying to get a one to many relationship up and
Rails and ActiveRecord do a perfectly nice job of dealing with what I would

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.