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

Related Questions

I'd like to run the rails generate script and create a model that belongs
Ok steps to reproduce this: prompt> rails test_app prompt> cd test_app prompt> script/generate model
I have created model, controller and view with rails scaffold generator: rails g scaffold
I'm developing a booking system in Rails 3.1. I have created a model for
During doing unit tests in Rails a few temp files are created (associated with
What's the best way to create a model in Ruby on Rails that doesn't
I'd like to create a callback function in rails that executes after a model
I created sample rails app and deployed into heroku. Rails version is 3.1.3 Ruby
hi I have two rails model class Feedback < ActiveRecord::Base has_many :conversations, :dependent =>
Hi there Im a beginner to rails and what Im doing seems a common

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.