【问题标题】:db:seed not loading modelsdb:seed 不加载模型
【发布时间】:2011-01-13 09:06:21
【问题描述】:

我正在尝试使用标准的db/seeds.rb 方法为我的数据库播种。这在我的开发机器上运行良好,但在我的服务器上,我得到:

$ sudo rake db:seed RAILS_ENV=production --trace
** Invoke db:seed (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:seed
rake aborted!
uninitialized constant Permission
/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2503:in `const_missing'
/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.4/lib/active_support/dependencies.rb:92:in `const_missing'
/path/.../.../.../.../db/seeds.rb:4
/usr/local/lib/ruby/gems/1.8/gems/rails-2.3.4/lib/tasks/databases.rake:215:in `load'
/usr/local/lib/ruby/gems/1.8/gems/rails-2.3.4/lib/tasks/databases.rake:215
/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in `call'
...

但是当我在控制台检查时,模型确实存在:

$ script/console production
Loading production environment (Rails 2.3.4)
>> Permission
=> Permission(id: integer, ..., created_at: datetime, updated_at: datetime)

我忘记了什么?

【问题讨论】:

  • 请注意,它在我的生产服务器上的开发环境中可以正常工作。
  • 你的seeds.rb是什么样子的?
  • 我修好了。我禁用了threadsafe!
  • 请添加您的答案并将其标记为已回答。

标签: mysql ruby-on-rails activerecord rake


【解决方案1】:

在对链接到上面和此处的博客的评论中:http://www.builtfromsource.com/2011/02/09/getting-rake-dbseed-and-config-threadsafe-to-play-nice/

布鲁斯·亚当斯提到可以打电话:

config.threadsafe!除非 $rails_rake_task

只在不运行 rake 任务时打开线程安全。

但由于问题确实是线程安全关闭了依赖加载,您可以简单地在 config.threadsafe 之后添加这一行!使其保持启用状态,但仍按预期加载您的环境。

config.dependency_loading = true if $rails_rake_task

【讨论】:

    【解决方案2】:

    可以通过在环境配置中禁用threadsafe!来修复。

    【讨论】:

      【解决方案3】:

      我刚刚遇到了解决这个问题的好方法in this article。我将在这里进行总结,以便人们(希望)更快地找到它。

      想法是在生产环境中关闭线程安全,首先通过编辑config/environments/production.rb:

      config.threadsafe! unless ENV['THREADSAFE'] == 'off'
      

      然后在运行 rake 任务时设置 THREADSAFE=off。

      【讨论】:

        猜你喜欢
        • 2012-11-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-27
        相关资源
        最近更新 更多