2013-06-27 87 views
2

添加新列这是我的用户model.rbRails的DB不迁移

class User < ActiveRecord::Base 
    attr_accessor :password 
    attr_accessible :email, :name 

    validates :name,:presence=>true,:length=>{:maximum=>15} 
    validates :email,:presence=>true,:length=>{:maximum=>15} 
end 

我想添加密码的新列。 我用命令

rails g migration pass_mig password:string 

然后

rake db:migrate 

但DB模式仍然

ActiveRecord::Schema.define(:version => 20130627073430) do 

     create_table "users", :force => true do |t| 
     t.string "name" 
     t.string "email" 
     t.datetime "created_at", :null => false 
     t.datetime "updated_at", :null => false 
     end 
end 
Rails中

而且控制台:密码不能在IE浏览器中的一个新的用户对象添加新的数据库条目..请建议。 P.S:我是新手,所以这可能是一个愚蠢的问题。我正在使用rails版本:3.2.13和ruby版本:1.9.3

回答

4

好的,这是关于大家都在讨论的rails的“神奇”事情之一。当您进行迁移以将列添加到表时,有一个命名约定。试试这个:

rails g migration add_password_to_users password:string 

其实几乎所有的命名约定都很重要。

+2

你还应该检查和编辑生成器生成的任何迁移文件,然后再执行它们。 – tadman

+0

嘿,非常感谢。“神奇”是。你也可以请告诉最好的地方来学习一些其他的魔法大会。 – user2526795

+1

user2526795你会想要按照@tadman上面提到的建议。并研究Bachan Smruty发布的代码。在rails中做大部分事情有多种方式。 –

0

您可以按照该添加独立迁移以下

rails generate migration AddPasswordToUsers password:string 

为多个独立的迁移,你可以按照链接。 http://guides.rubyonrails.org/migrations.html#creating-a-standalone-migration

或者你可以做另一件事。 月1日创建迁移

rails g migration AddColumnToUser 

这将创建一个数据库迁移文件/迁移。您需要更改该文件内容。

class AddColumnToUser < ActiveRecord::Migration 
    def change 
    # all the codes being generated automatically. The following you need to write. 
    add_column :users, :password, :string # this means you are adding password having string type in users table. 
    end 
end 

完成上述步骤之一。只是做rake db:migrate

+1

嗨..感谢...非常有帮助.. – user2526795