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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With