我正在向数据库中的现有表添加一列。迁移在此处创建:
// ♥ rails g migration AddFieldToRecipes
Running via Spring preloader in process 80952
invoke active_record
create db/migrate/20180628220935_add_field_to_recipes.rb
在此处编辑:
class AddFieldToRecipes < ActiveRecord::Migration[5.2]
def change
add_column :recipes, :cooked, :boolean, null: false, default: false
end
end
在这里运行:
// ♥ rails db:migrate
== 20180628220935 AddFieldToRecipes: migrating
================================
-- add_column(:recipes, :cooked, :boolean, {:null=>false,
:default=>false})
-> 0.0079s
== 20180628220935 AddFieldToRecipes: migrated (0.0080s)
=======================
是否更改架构:
create_table "recipes", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "cooked", default: false, null: false
end
运行时ActiveRecord会看到更改:
ActiveRecord::Base.connection.columns("recipes")
=> [#<ActiveRecord::ConnectionAdapters::PostgreSQLColumn:0x00007f8b69f91018
@collation=nil,
@comment=nil,
@default=nil,
@default_function="nextval('recipes_id_seq'::regclass)",
@max_identifier_length=63,
@name="id",
@null=false,
@sql_type_metadata=
#<ActiveRecord::ConnectionAdapters::SqlTypeMetadata:0x00007f8b69f912c0 @limit=8, @precision=nil, @scale=nil, @sql_type="bigint", @type=:integer>,
@table_name="recipes">,
#<ActiveRecord::ConnectionAdapters::PostgreSQLColumn:0x00007f8b69f905c8
@collation=nil,
@comment=nil,
@default=nil,
@default_function=nil,
@max_identifier_length=63,
@name="created_at",
@null=false,
@sql_type_metadata=
#<ActiveRecord::ConnectionAdapters::SqlTypeMetadata:0x00007f8b69f90960
@limit=nil,
@precision=nil,
@scale=nil,
@sql_type="timestamp without time zone",
@type=:datetime>,
@table_name="recipes">,
#<ActiveRecord::ConnectionAdapters::PostgreSQLColumn:0x00007f8b6ad6f860
@collation=nil,
@comment=nil,
@default=nil,
@default_function=nil,
@max_identifier_length=63,
@name="updated_at",
@null=false,
@sql_type_metadata=
#<ActiveRecord::ConnectionAdapters::SqlTypeMetadata:0x00007f8b6ad6fc48
@limit=nil,
@precision=nil,
@scale=nil,
@sql_type="timestamp without time zone",
@type=:datetime>,
@table_name="recipes">,
#<ActiveRecord::ConnectionAdapters::PostgreSQLColumn:0x00007f8b6ad6edc0
@collation=nil,
@comment=nil,
@default="false",
@default_function=nil,
@max_identifier_length=63,
@name="cooked",
@null=false,
@sql_type_metadata=
#<ActiveRecord::ConnectionAdapters::SqlTypeMetadata:0x00007f8b6ad6ef78 @limit=nil, @precision=nil, @scale=nil, @sql_type="boolean", @type=:boolean>,
@table_name="recipes">]
但在控制台(或在应用程序中)时不会反映在模型上:
[1] pry(main)> Recipe.columns.map { |c| c.name }
=> ["id", "title", "description", "author", "user_id", "type", "created_at", "updated_at"]
所以我尝试重置列信息:
[2] pry(main)> Recipe.reset_column_information
=> {true=>#<Concurrent::Map:0x007f8b69681cc0 entries=0 default_proc=nil>, false=>#<Concurrent::Map:0x007f8b69681c20 entries=0 default_proc=nil>}
仍然没有变化:
[3] pry(main)> Recipe.columns.map { |c| c.name }
=> ["id", "title", "description", "author", "user_id", "type", "created_at", "updated_at"]
基于此处找到的许多(许多)问题/答案(也是the same exact question with no answer的另一个问题),我回滚了迁移并重新运行了它-同样。回滚,然后重新加载架构,重新运行-相同。由于是开发人员,因此我也删除了数据库,创建,迁移了相同的数据库。
我想念的是什么?我只想让recipe.cooked
向我显示是非。