gpt4 book ai didi

ruby-on-rails - 向模型添加新列

转载 作者:太空宇宙 更新时间:2023-11-03 17:07:27 24 4
gpt4 key购买 nike

尝试将名字和姓氏添加到现有模型用户

首先这样做:

rails g migration first_name_to_users first_name:string

然后这样做:

rails g migration last_name_to_users last_name:string

显然运行了 rake db:migrate 结果是这样的:

== FirstNameToUsers: migrating =============================================== == FirstNameToUsers: migrated (0.0000s) ======================================

== LastNameToUsers: migrating ================================================ == LastNameToUsers: migrated (0.0000s) =======================================

但它并没有出现在表格中!

如果我进入 Rails 控制台并运行 User.column_names,我得到这个:

=> ["id", "email", "encrypted_password", "reset_password_token", "reset_password_sent_at", "remember_created_at", "sign_in_count", "current_sign_in_at", "last_sign_in_at", "current_sign_in_ip", "last_sign_in_ip", "created_at", "updated_at"] 

如果有人问它是否出现在 db/migrate 文件夹中,答案是肯定的。

这是姓氏:

class LastNameToUsers < ActiveRecord::Migration
def change
end
end

那么......为什么它没有出现在表格中?

最佳答案

在一个迁移文件中执行此操作,

rails g migration add_columns_to_users

然后在使用上述命令生成的迁移文件中执行此操作:-

class AddColumnsToUsers < ActiveRecord::Migration
def change
add_column :users, :first_name, :string
add_column :users, :last_name, :string
end
end

关于ruby-on-rails - 向模型添加新列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22551077/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com