Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 3 添加:默认值=>;true为现有Rails列中的布尔值_Ruby On Rails 3_Migration - Fatal编程技术网

Ruby on rails 3 添加:默认值=>;true为现有Rails列中的布尔值

Ruby on rails 3 添加:默认值=>;true为现有Rails列中的布尔值,ruby-on-rails-3,migration,Ruby On Rails 3,Migration,我在这里看到了一些关于向现有列添加默认布尔值的问题。因此,我尝试了change\u列的建议,但我一定做得不对 我试过: $ change_column :profiles, :show_attribute, :boolean, :default => true 返回-bash:change\u列:未找到命令 然后我跑: $ rails g change_column :profiles, :show_attribute, :boolean, :default => true …和

我在这里看到了一些关于向现有列添加默认布尔值的问题。因此,我尝试了
change\u列的建议,但我一定做得不对

我试过:

$ change_column :profiles, :show_attribute, :boolean, :default => true
返回
-bash:change\u列:未找到命令

然后我跑:

$ rails g change_column :profiles, :show_attribute, :boolean, :default => true
…和

$ rails change_column :profiles, :show_attribute, :boolean, :default => true
然后运行
rakedb:migrate
,但
:show_属性的值保持为
nil
。在我上面提到的问题中,它说在PostgreSQL中,您需要手动更新它。由于我使用的是PostgreSQL,所以在我的
create\u profiles
migration中添加了以下内容:

t.boolean :show_attribute, :default => true

有人能告诉我我做错了什么吗?

change\u column
是一种
ActiveRecord::Migration
的方法,所以您不能在控制台中这样调用它

如果要为此列添加默认值,请创建新迁移:

t.boolean :show_attribute, :default => true
rails g迁移将默认值添加到显示属性中

然后在创建的迁移中:

# That's the more generic way to change a column
def up
  change_column :profiles, :show_attribute, :boolean, default: true
end

def down
  change_column :profiles, :show_attribute, :boolean, default: nil
end
或者更具体的选择:

def up
    change_column_default :profiles, :show_attribute, true
end

def down
    change_column_default :profiles, :show_attribute, nil
end
然后运行
rakedb:migrate

它不会对已创建的记录进行任何更改。要做到这一点,您必须创建一个
rake任务
,或者只需进入
rails控制台
并更新所有记录(我不建议在生产中使用)


当您将
t.boolean:show_属性,:default=>true
添加到
create_profiles
迁移中时,预期它不会执行任何操作。只执行尚未运行的迁移。如果您从一个新的数据库开始,那么它会将默认值设置为true。

作为接受答案的变体,您还可以在迁移中使用
更改列\u默认值
方法:

def up
  change_column_default :profiles, :show_attribute, true
end

def down
  change_column_default :profiles, :show_attribute, nil
end

我不确定这是什么时候写的,但当前要在迁移中添加或删除列的默认值,可以使用以下方法:

change_column_null :products, :name, false
轨道5:

change_column_default :products, :approved, from: true, to: false

第4.2条:

change_column_default :products, :approved, false

这是一种避免在迁移或模式中查找列规范的简洁方法

change_column :things, :price_1, :integer, default: 123, null: false
似乎是向现有列中添加默认值的最佳方法,该列没有
null:false

否则:

change_column :things, :price_1, :integer, default: 123
我在这方面做了一些研究:


如果您刚刚进行了迁移,您可以回滚,然后再次进行迁移

要回滚,您可以执行任意多个步骤:

rake db:rollback STEP=1
或者,如果您使用的是Rails 5.2或更高版本:

rails db:rollback STEP=1
然后,您可以再次进行迁移:

def change
  add_column :profiles, :show_attribute, :boolean, default: true
end
别忘了
rake db:migrate
,如果您使用的是heroku
heroku,也可以运行rake db:migrate

,请参见以下文档:

无法通过命令行指定默认值

所以没有现成的rails生成器。按照上述答案的规定,您必须使用
change\u column\u default
方法手动填充迁移文件


您可以创建自己的生成器:

如果您不想为最近的小更改创建另一个迁移文件-从Rails控制台:

ActiveRecord::Migration.change_column :profiles, :show_attribute, :boolean, :default => true
然后退出并重新进入rails控制台,以便DB更改生效。那么,如果你这样做

Profile.new()
您应该看到“show_attribute”默认值为true

对于现有记录,如果要保留现有的“false”设置并仅将“nil”值更新为新的默认值:

Profile.all.each{|profile| profile.update_attributes(:show_attribute => (profile.show_attribute == nil ? true : false))  }
更新创建此表的迁移,以便将来的任何DB构建都能从一开始就得到它。还可以在数据库的任何已部署实例上运行相同的进程


如果使用“new db migration”方法,您可以在该迁移中更新现有的nil值。

该更改列调用应该在迁移中的
up
方法中,这是将在db/migrate/中生成的新类。(应该编写
down
方法来撤销
up
所做的操作。)进行更改,然后
rake db:migrate
。啊,这更有意义。谢谢在我编写
def self.up
def self.down
之前,它对我不起作用,那时你可能正在使用旧版本的rails。我认为这种语法是从3.1.1开始的,在Rails 5中,您去掉了_属性,所以它应该只说
show
或任何列名。这确保您不会意外地更改任何其他列属性。在Rails 5中,您去掉了_属性,所以它应该只说
show
或任何列名。@迷宫您是什么意思
show_attribute
是该列的名称,我认为rails 5与此无关,对吗?请注意,它来自rails 5文档。Rails 4.2版本不接受散列,但接受新的默认值作为第三个参数。关于Rails 5,这两种方法似乎都是最正确的,例如
null:false
default::something