Ruby on rails Rails db不使用迁移添加新列
这是我的用户模型.rbRuby on rails Rails db不使用迁移添加新列,ruby-on-rails,ruby-on-rails-3.2,Ruby On Rails,Ruby On Rails 3.2,这是我的用户模型.rb 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
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
然后
但在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控制台中:密码不能添加到新的用户对象中,即新的数据库条目中。请建议。
我是rails的新手,所以这可能是一个愚蠢的问题。我正在使用rails版本:3.2.13和ruby版本:1.9.3好的,这是每个人都在谈论的关于rails的“神奇”东西之一。在迁移以向表中添加列时,有一个命名约定。试试这个:
rails g migration add_password_to_users password:string
实际上,几乎所有东西都有重要的命名约定 您可以按照以下步骤添加独立迁移
rails generate migration AddPasswordToUsers password:string
要获得更多的独立迁移,您可以点击链接。
或者你可以做另一件事。
首先创建一个迁移
rails g migration AddColumnToUser
这将在db/migrate中创建迁移文件。您需要更改该文件的内容
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
class AddColumnToUser
完成上述步骤之一后。只需执行
rake db:migrate
您还应该在执行生成器生成的任何迁移文件之前检查和编辑它们。嘿,非常感谢。这很神奇。另外,你能告诉我从user2526795学习其他魔法惯例的最佳地点吗?你会想按照上面提到的@tadman的建议去做。并研究Bachan Smruty发布的代码。在rails中,有多种方法可以完成大部分工作。如果您想了解更多关于rails如何工作的建议,有很多方法可以提供您想要的示例。@tadman ok..谢谢。。看起来正是我要找的东西
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