【问题标题】:mongo load_models giving error when using enum inside STI in rails在 Rails 中的 STI 中使用枚举时,mongo load_models 出错
【发布时间】:2016-08-02 19:30:12
【问题描述】:

我在 rails 中使用单表继承。

module ModuleName
  class ParentClass < ActiveRecord::Base
    enum status: {OPEN: 'open', CLOSED: 'closed'}
  end
end

module ModuleName
  class ChildClass1 < ParentClass
  end
end

module ModuleName
  class ChildClass2 < ParentClass
  end
end

迁移和本地主机服务器运行良好,但是在执行命令时:-

bundle exec rake db:mongoid:remove_indexes

bundle exec rake db:mongoid:create_indexes

我遇到了错误。

ArgumentError: You tried to define an enum named "status" on the model "ModuleName::ParentClass", but this will generate a instance method "status=", which is already defined by another enum.
Tasks: TOP => db:mongoid:remove_indexes => db:mongoid:load_models

作为一次成功的尝试,我尝试从父类中删除状态枚举,并将它们分别放入子类中。没用。

在互联网上找不到任何相关帮助。 任何帮助或建议表示赞赏。 谢谢。

【问题讨论】:

    标签: ruby-on-rails ruby mongodb enums


    【解决方案1】:

    修复它。

    只要模型被预先加载,就会出现此问题。 db:mongoid:load_models 就是这样做的。如果你有,迁移和 Rails 服务器也会失败

    config.eager_load = true
    

    在你的配置文件中

    解决方案更像是一种解决方法。它通过将枚举声明为:-

    enum status: {OPEN: 'open', CLOSED: 'closed'} unless instance_methods.include? :status
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-10
      • 1970-01-01
      • 1970-01-01
      • 2016-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多