2013-02-07 40 views
0

我有更新迁移脚本下DB /迁移和Ruby on Rails的:如何使修改数据库模型

class CreateStudents < ActiveRecord::Migration 
    def change 
    create_table :students do |t| 
     t.string :firstname 
     t.string :lastname  
     t.string :account 
     t.timestamps 
    end 
    end 
end 

DATABSE剧本后,我做了一个

rake db:migrate 

数据库脚本更新之前更新

class CreateStudents < ActiveRecord::Migration 
    def change 
    create_table :students do |t| 
     t.string :firstname 
     t.string :lastname  
     t.string :account 
     t.string :address  
     t.string :city 
     t.string :state 
     t.string :postcode       
     t.string :homephone 
     t.timestamps 
    end 
    end 
end 

我丢弃了旧的development.sqlite3和旧的schema在schame.rb中。
说我添加了几列,但在模型中这些列丢失。

但我的模式仍然是

class Student < ActiveRecord::Base 
    attr_accessible :firstname,:lastname,:account, 
end 

有一个简单的方法使我能在新的迁移脚本修改模型?

+1

你的意思是什么样的变化? – eeeeeean

+0

@eeeeeean说我添加了几列,但在模型中这些列丢失 – icn

+0

给出的例子。你的问题含糊不清。 – Huy

回答

1

看起来你可能做了rails generate migration这不会影响你的模型。我相信在创建模型之后,所有事情都必须手动完成。

如果你真的想要同时改变你的数据库和模型,你最好的办法可能是删除你的迁移和模型,并从头开始创建你的整个脚手架(rails generate scaffold)(documentation)。

0

在模型中手动添加新列没有问题。

2

如果要允许其他属性的质量分配,你可以添加键attr_accessible

class Student < ActiveRecord::Base 
    attr_accessible :firstname,:lastname,:account,:address, :city, :state, :postcode, :homephone 
end 

但是,你的模型仍然有那些属性(或列如你给他们打电话)。你不能做一个大规模的任务(比如create或者update_attributes),而不是先让它们attr_accessible。