[英]How I can change the model attribute name on ruby on rails
I want to change false variable name to new one, so I created the true and new one but the old and false one stil stay!
我想将错误的变量名更改为新变量,所以我创建了真实和新变量,但旧的和错误的变量仍然存在!
How can remove the false one
如何去除假的
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>
I made a typo mistake it must be "completed"
我错了一个错字,必须“完成”
What can I do? 我能做什么?
rails g migration remove_complated_at_from_items complated_at:datetime
this line generate those codes
这行生成那些代码
class RemoveComplatedAtFromItems < ActiveRecord::Migration[5.2]
def change
remove_column :items, :complated_at, :datetime
end
end
my current schema.rb file
我当前的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
now lets run rails db:migrate
现在让我们运行rails db:migrate
$ rails db:migrate
== 20190227204841 RemoveComplatedAtFromItems: migrating =======================
-- remove_column(:items, :complated_at, :datetime)
-> 0.0038s
== 20190227204841 RemoveComplatedAtFromItems: migrated (0.0039s) ==============
Seems every things is okay!
似乎一切都还好! lets check it schema.rb!
让我们检查一下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
All is good right now.
现在一切都很好。 Last to left lets check the one object!
从左至左检查一个对象!
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>
DONE!
完成!
Create a migration and use the remove_column
method to alter your database. 创建迁移,并使用
remove_column
方法更改数据库。
https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-remove_column https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-remove_column
Don't delete column. 不要删除列。 Just rename it.
只需重命名即可。
Create a new migration and put the code: 创建一个新的迁移并放入代码:
rename_column :items, :complated_at, :completed_at
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.