本文介绍了ActiveRecord属性消失的情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照 https://stackoverflow.com/a/24496452/102675 中的说明进行操作,我总结了以下内容:

Following the instructions in https://stackoverflow.com/a/24496452/102675 I wound up with the following:

namespace :db do
  desc 'Drop, create, migrate, seed and populate sample data'
  task seed_sample_data: [:drop, :create, :migrate, :seed, :populate_sample_data] do
    puts 'Sample Data Populated. Ready to go!'
  end

  desc 'Populate the database with sample data'
  task populate_sample_data: :environment do
    puts Inspector.column_names.include?('how_to_fix')
    # create my sample data
  end
end

如您所料,如果运行bundle exec rake db:populate_sample_data

但是如果我运行bundle exec rake db:seed_sample_data,我会得到所有迁移输出,然后是false.换句话说,即使通过其他前几次测试证明它确实存在,我也看不到Inspector属性how_to_fix.我的属性去哪儿了?

BUT if I run bundle exec rake db:seed_sample_data I get all the migration output and then false. In other words I can't see the Inspector attribute how_to_fix even though it definitely exists as proved by the other rake run. Where did my attribute go?

推荐答案

我的猜测是这是一个缓存"问题.您可以尝试以下吗?

My guess is that this is a "caching" problem. Can you try the following?

task populate_sample_data: :environment do
  Inspector.reset_column_information
  # ...
end

P.S.我们曾经在使用具有完全相同架构的不同数据库(除了此处和此处的某些列除外)时使用过类似的问题

这篇关于ActiveRecord属性消失的情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 22:35