如何在 rails 上更改 ruby 上的模型属性名称
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"
我能做什么?
创建迁移并使用 remove_column
方法更改数据库。
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
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
== 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!
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!
不要删除列。只需重命名即可。
创建一个新的迁移并放置代码:
rename_column :items, :complated_at, :completed_at
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"
我能做什么?
创建迁移并使用 remove_column
方法更改数据库。
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
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
== 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!
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!
不要删除列。只需重命名即可。
创建一个新的迁移并放置代码:
rename_column :items, :complated_at, :completed_at