我想将错误的变量名更改为新变量,因此我创建了真实和新变量,但旧的和错误的变量仍然存在!
如何删除假的
irb(main):001:0> item = Item.last
Item Load (0.3ms) SELECT "items".* FROM "items" ORDER BY "items"."id" DESC LIMIT ? [["LIMIT", 1]]
=> #<Item id: 6, title: "Make a cake for your darling", description: "She loves furit ", created_at: "2019-02-27 18:04:15", updated_at: "2019-02-27 18:04:15", user_id: 2, complated_at: nil, completed_at: nil>
irb(main):002:0>
我错了一个错字,必须“完整”
我该怎么办?
答案 0 :(得分:2)
rails g migration remove_complated_at_from_items complated_at:datetime
此行生成这些代码
class RemoveComplatedAtFromItems < ActiveRecord::Migration[5.2]
def change
remove_column :items, :complated_at, :datetime
end
end
我当前的schema.rb文件
ActiveRecord::Schema.define(version: 2019_02_27_204841) do
create_table "items", force: :cascade do |t|
t.string "title"
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
t.datetime "completed_at"
t.datetime "complated_at"
end
现在让我们运行rails db:migrate
$ rails db:migrate
== 20190227204841 RemoveComplatedAtFromItems: migrating =======================
-- remove_column(:items, :complated_at, :datetime)
-> 0.0038s
== 20190227204841 RemoveComplatedAtFromItems: migrated (0.0039s) ==============
似乎一切都还好!让我们检查一下schema.rb!
ActiveRecord::Schema.define(version: 2019_02_27_204841) do
create_table "items", force: :cascade do |t|
t.string "title"
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
t.datetime "completed_at"
end
现在一切都很好。从左至左检查一个对象!
rails c
irb(main):001:0> item = Item.last
Item Load (0.3ms) SELECT "items".* FROM "items" ORDER BY "items"."id" DESC LIMIT ? [["LIMIT", 1]]
=> #<Item id: 6, title: "Make a cake for your darling", description: "She loves furit ", created_at: "2019-02-27 18:04:15", updated_at: "2019-02-27 18:04:15", user_id: 2, completed_at: nil>
完成!
答案 1 :(得分:0)
创建迁移并使用remove_column
方法来更改数据库。
答案 2 :(得分:0)
不删除列。只需重命名即可。
创建一个新的迁移并放入代码:
rename_column :items, :complated_at, :completed_at