我正在使用 Rails 3.2,并且有一个包含代码的迁移:
add_column :users, :gift_aid, :integer, :default => 2
# reset columns
User.reset_column_information
... code here to load legacy data from sqlite3 database ...
# now create a user with the loaded column data
user = User.create( ...other cols...,
:gift_aid => migrated_gift_aid_column_data,
...other cols... )
并且在运行迁移时我得到未知属性:gift_aid
。 User.column_names
在调用 reset_column_information
之前和之后显示相同的列表。
奇怪的是,当我手动删除 mysql 中的列并重新运行迁移时,它按预期工作。使用空数据库再次从第一次迁移开始,但它不起作用,因此这与运行所有迁移而不是单个迁移有关。
我之前对用户模型进行了几次迁移,都包含 reset_column_information
并且都工作正常。
我真的对此摸不着头脑 - 大家有什么想法
最佳答案
我认为这一定是某种与模式缓存相关的错误......这可能有效:
User.connection.schema_cache.clear!
User.reset_column_information
(适用于 Rails 3.2.2)
关于ruby-on-rails - Model.reset_column_information 不会在 Rails 迁移中重新加载列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9115347/