我有一个类似以下的 mongoid 模型和方法:
class Category
include Mongoid::Document
field :name, :type => String
...
def self.custom_find
...
Str = Struct.new(:arg1, :arg2)
array << Str.new(one, two)
...
end
end
我收到以下错误:
dynamic constant assignment (SyntaxError)
Str = Struct.new(:arg1, :arg2)
我尝试Str = Struct.new(:arg1, :arg2)
进入 config/initializers/categories.rb 但后来我得到:
BSON::InvalidDocument in TrendsController#index
Cannot serialize an object of class Category into BSON.
通过删除 custom_find 违规方法,其他一切都可以正常工作,那么我猜是与"Struct"
.
任何想法 ?