Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Normalizing data in rails

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?

like image 759
petehern Avatar asked Dec 12 '22 22:12

petehern


1 Answers

I'm not aware of a built-in Rails way to programmatically split up your model. You'll need to write a new Address model and database update migration to get everything switched over.

Your models will likely look something like this:

class Person < ActiveRecord::Base
  has_many :addresses
end

class Address < ActiveRecord::Base
  belongs_to :person
end
like image 99
maček Avatar answered Dec 30 '22 17:12

maček