【问题标题】:should I restart Rails App? if I want to use enum from db?我应该重新启动 Rails 应用程序吗?如果我想使用数据库中的枚举?
【发布时间】:2016-07-14 09:53:16
【问题描述】:

我想将枚举用于选择选项,值来自数据库。 就像下面一样。 键是“id”,值是“名称”

COUNTRY = Country.select("name", "id").map {|item| [item.name.to_sym, item.id] }.to_h 枚举 country_id: 国家 def country_id_enum 国家 结束

但是在我创建,创建更新删除方法之后,我意识到如果我不重新启动rails应用程序,数据将不会被应用。(服务httpd重启)

有什么解决办法吗?

【问题讨论】:

    标签: ruby-on-rails ruby activerecord


    【解决方案1】:

    你应该写方法访问器,而不是常量

    class Dictionaries
      def countries
        #load and return what you need here.
      end
    end
    

    【讨论】:

    • 你能给我更多的例子吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-15
    • 1970-01-01
    • 1970-01-01
    • 2010-11-07
    • 2012-10-24
    • 1970-01-01
    • 2010-12-21
    相关资源
    最近更新 更多