The case of the disappearing ActiveRecord attribute

邮差的信 提交于 2019-12-25 09:28:16

问题


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

As you would expect, I get true if I run bundle exec rake db:populate_sample_data

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?


回答1:


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. We used to have a similar problem working with different databases having the exact same schema (only except some columns here and there)



来源:https://stackoverflow.com/questions/44381014/the-case-of-the-disappearing-activerecord-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!