0

私は次のようなモンゴイドモデルとメソッドを持っています:

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 / category.rbに移動しようとしましたが、次のようになります。

BSON::InvalidDocument in TrendsController#index

Cannot serialize an object of class Category into BSON.

custom_findの問題のあるメソッドを削除することで、他のすべてが正常に機能するようになります。これはに関連していると思い"Struct"ます。

何か案が ?

4

1 に答える 1

2

動的定数代入エラーは、いくつかの方法で回避できます。

array = Struct.new(:arg1, :arg2).new(one, two)

また

Object.const_set :Str, Struct.new(:arg1, :arg2)
Str # => Str

ただし、これでシリアル化の問題が解決するわけではありません。

于 2012-04-13T18:23:34.630 に答える