我有一个我不了解的问题,无法找到解决方案:
works:
class Document
CONSTANT_ARRAY = [0,1,2,3]
delegate :sum, to: :CONSTANT_ARRAY
end
does not work:
class Document
include Mongoid::Document
CONSTANT_ARRAY = [0,1,2,3]
delegate :sum, to: :CONSTANT_ARRAY
end
后者抛出错误ArgumentError:参数数量错误(给定2,预期为1)
要添加的是,代码在mongoid升级之前已经运行,在版本〜> 5.0,rails 4中,现在我具有mongoid 7.1.0,rails 5.2.4.1]
我不确定是否与添加有关,代码将从另一个类中调用
class Items
include Mongoid::Document
embeds_many :document_fields, class_name: 'Document', cascade_callbacks: true
end
class Another
include Mongoid::Document
embeds_many :items, class_name: 'Item', cascade_callbacks: true
def document_fields
items.flat_map(&:document_fields)
end
end
我减少了类中的代码量,因为我看不到相关性。
更新:所以我发现这可行。但这是正确的方法吗?
CONSTANT_ARRAY = [0,1,2,3]
delegate :sum => :CONSTANT_ARRAY
logger.debug Document.new.sum # prints 6 as it is supposed to
这是Mongoid 7.1.0中的一个问题:https://jira.mongodb.org/browse/MONGOID-4849