2016-08-02 79 views
0

我在rails中使用了Singe表繼承。mongo load_models在STI中使用枚舉內部導軌時出錯

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 

作爲命中和試用,我嘗試從父類中移除狀態枚舉,並將它們分別放入子類中。不工作。

在因特網上找不到任何相關幫助。 任何幫助或建議表示讚賞。 謝謝。

回答

0

修正了它。

無論何時模型被預先加載,都會發生該問題。 db:mongoid:load_models這樣做。如果你有

config.eager_load = true 

在您的配置文件

解決方案更多的是一種解決方法的遷移和Rails服務器會失敗過。它的工作原理是聲明枚舉爲: -

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