为什么一个方法调用一个?一下子就把参数传递给它了吗?仅供参考,我更新到Mongoid 5.0
在下面的迹线中表示单例的第18行是问题。我不确定它是如何到达该行的,因为mongodb中有1个Settings对象,所以它不应该创建一个?
对象:
class Settings
include Mongoid::Document
field :use_kue, :type => Boolean, :default => false
field :api_url, :type => String
field :online_reason, :type => String
def self.singleton
s = Settings.first
if s.nil?
s = Settings.create <---- the line causing the issue
end
s
end
def self.use_kue?
s = Settings.singleton
s.use_kue
end
end
致电:
if Settings.use_kue?
.....
end
堆栈跟踪:
Started GET "/ems/admin" for 70.124.147.246 at 2019-05-06 21:57:14 +0000
May 06 16:57:15 app/web.1: Processing by EmController#admin as HTML
May 06 16:57:15 app/web.1: Completed 500 Internal Server Error in 8ms
May 06 16:57:15 app/web.1: NameError (undefined local variable or method `args' for SendGrid::Settings:Class):
May 06 16:57:15 app/web.1: app/models/settings.rb:18:in `singleton'
May 06 16:57:15 app/web.1: app/controllers/em_controller.rb:45:in `admin'
修复:
使用以下Ruby,rails或MongoID的东西不喜欢以's'命名的模型(设置代替设置起作用)。我猜1.9,Rails 4和旧的MongoID更宽容。
红宝石“ 2.4.2” gem'rails','〜> 4.2.8' 宝石'mongoid','〜> 5.0.0'