0

だから、私は Ruby MongoDB ドライバーを使用しており、次のように挿入してオブジェクトを作成したいと考えています。

 db.insert_one({
  'game_id' => @token,
  'board' => {
    'tiles' => @board.tiles
  }
})

@board は Board クラスのインスタンスです。

class Board
 attr_accessor :tiles
  def initialize()
    @tiles = [Tile.new, Tile.new]
  end
end

とタイル

class Tile
  def initialize()
    @x = 1, @y = 1
  end
  def to_json(options)
    {"x" => @x, "y" => @y}.to_json
  end
end

最後に、「タイル」フィールドは次のようになります。

'tiles': [{x:1, y:1}, {x:1, y:1}]

次のエラーが表示されます。

undefined method `bson_type' for #<Tile:0x007ff7148d2440>

私が使用している宝石: 'sinatra'、'mongo (2.0.4)'、および 'bson_ext' (すべて Bundler.require を使用して必要です)。ありがとう!

4

1 に答える 1