2

プロジェクトでmongoid gemの使用を開始しましたが、データベースに情報を保存して取得する方法について少し混乱しています。モデルに特定のタイプのフィールドがありますが、DB から取得するとハッシュが返されます。ここに私のモデルがあります:

service.rb

class Service
  include Mongoid::Document
  field :username, type: String
  field :strategy, type: Strategy
  field :design, type: Design

end

戦略.rb

class Strategy 
  include Mongoid::Document

  field :name, type: String
  field :description, type:  String
  field :resources, type:  Resources
  field :scalability, type:  Scalability  
  field :localization, type:  Localization
  field :contact, type:  Contact

end

新しいサービス @service を初期化して @service.class を実行すると、適切な Service が返されますが、@service.strategy.class を実行しようとすると、予想どおり、Strategy ではなく Hash が返されます。私はモンゴイドのマニュアルを読んで、「カスタムフィールドのシリアライゼーション」があり、私がやりたいことができると思います。しかし、変更するモデルがたくさんあるので、それを簡単に行う方法が他にないかどうか疑問に思っていました。

4

0 に答える 0