Rails
版本:6.0.2.2
Ruby
版本:2.7.0p0
gem 'rails-settings-cached', '~> 2.2'
通过 field
在 setting.rb
里设置参数是可以通过 Setting.var
获取到正确的值:
# setting.rb
...
cache_prefix { "setting_cache" }
field :check_hour, default: 12, type: :integer
...
2.7.0 :001 > Setting.check_hour
=> 12
当我通过settings_controller.rb
来创建值后,在 rails 缓存里有该字段,但是不能通过Setting.var
获取到值
# settings_controller.rb
class Admins::SettingsController < Admins::AdminsController
def create
setting = Setting.new(setting_params)
if setting.save
tip_status(1, "创建成功")
else
tip_status(-1, "创建失败")
end
end
private
def setting_params
params.require(:setting).permit(:var, :value)
end
end
# 新增hello记录后,在cache里可以查到该记录
2.7.0 :003 > Rails.cache.fetch("rails-settings-cached/setting_cache")
=> {"check_hour"=>12, "hello"=>"good"}
# 通过Setting.var,会出现报错
2.7.0 :004 > Setting.hello
NoMethodError (undefined method `hello' for #<Class:0x00007fe59ded68d8>)
请问各位大佬,我这是哪里有问题呢?该怎么解决呢?非常感谢~~