Rails 数据库迁移,未定义方法“to_sym”,无法弄清楚语法

发布于 2024-08-31 15:43:34 字数 1084 浏览 0 评论 0原文

这是我原来的迁移:

class CreateUsers < ActiveRecord::Migration
  def self.up
    ActiveRecord::Base.transaction do
      create_table "users", :force => true do |t|
        t.string :login, :limit => 40
        t.string :name, :limit => 100, :default => '', :null => true
        t.string :email, :limit => 100
        t.string :crypted_password, :limit => 40
        t.string :salt, :limit => 40
        t.string :remember_token, :limit => 40
        t.datetime :remember_token_expires_at
        t.string :activation_code, :limit => 40
        t.datetime :activated_at, :datetime
        t.string :state, :null => :no, :default => 'passive'
        t.datetime :deleted_at
        t.integer :occupation_id, :null => :yes
        t.datetime :paid_up_to_date, :date
        t.timestamps
      end

我正在尝试将“状态”的默认值更改为“主动”而不是被动 这是我的第二次尝试;

class ChangeUserStateDefault < ActiveRecord::Migration
  def self.up
    change_column :users, :state, :null => :no, :default => 'active'
end

Here is my original migration:

class CreateUsers < ActiveRecord::Migration
  def self.up
    ActiveRecord::Base.transaction do
      create_table "users", :force => true do |t|
        t.string :login, :limit => 40
        t.string :name, :limit => 100, :default => '', :null => true
        t.string :email, :limit => 100
        t.string :crypted_password, :limit => 40
        t.string :salt, :limit => 40
        t.string :remember_token, :limit => 40
        t.datetime :remember_token_expires_at
        t.string :activation_code, :limit => 40
        t.datetime :activated_at, :datetime
        t.string :state, :null => :no, :default => 'passive'
        t.datetime :deleted_at
        t.integer :occupation_id, :null => :yes
        t.datetime :paid_up_to_date, :date
        t.timestamps
      end

I am trying to change the default of "state" to be "active" instead of passive
Here is my second attempt;

class ChangeUserStateDefault < ActiveRecord::Migration
  def self.up
    change_column :users, :state, :null => :no, :default => 'active'
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

另类 2024-09-07 15:43:34

编辑:

错误是因为您缺少列的类型。
用法

change_column(表名、列名、类型、选项 = {})

所以这应该适合你:

change_column :users, :state, :string, :null => false, :default => 'active'

EDIT:

The error is because you were missing the type of the column.
Usage:

change_column(table_name, column_name, type, options = {})

So this should work for you:

change_column :users, :state, :string, :null => false, :default => 'active'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文