
script/generate model Customer name:string address:string city:string state:string zip:integer [...]
我用5000个客户填充了数据库并开始构建我的应用程序.现在我意识到我的模型没有规范化:我经常在同一个地址有多个客户!如果我希望按地址执行某些 *** 作,例如邮件,则会导致问题.我想要的是地址模型,客户模型和邮件模型.
是否有轨道方式来规范现有模型,将其分为两个模型?或者我应该编写一个脚本来规范化现有数据,然后相应地生成新模型?
解决方法 您询问了迁移的样子.我没有在评论回复中填写此内容,而是为您创建了一个新答案.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 endend 资源
> AcitveRecord::Migration
> execute
以上是内存溢出为你收集整理的ruby-on-rails – 规范化rails中的数据全部内容,希望文章能够帮你解决ruby-on-rails – 规范化rails中的数据所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)