Я добавляю столбец к существующей таблице в моей БД. Миграция, созданная здесь:
// ♥ 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"]
Основываясь на множестве (многих) вопросов / ответов, найденных здесь (также другой из того же самого точного вопроса без ответа ), я откатил миграцию и перезапустил ее - тоже самое. Откатился, и перезагрузил схему, реран - то же самое. Так как это dev, я также сбросил БД, создал, перенес - тоже самое.
Чего мне не хватает? Я просто хочу, чтобы recipe.cooked
показывал мне истину или ложь.