2011-01-18 10 views
2

私はオームモデルのツリーを持っています。ゲームにはプレーヤーがあり、プレーヤーにはピースがあります。詳細は以下の通りです。私は、JSONのような構造をレンダリングするために行くとき、基本的に、私は、エンコーディングエラーを参照してください。オームモデルツリーをJSON構造に変換する

NoMethodError (undefined method `encode_json' for #Piece:0x00000102b8dbb8):

しかし、私はエラーなしで出力Playerとその作品をすることができます。

Player = Player.create(:name => "Toby") 
game.player << player 

player.pieces << Piece.create(:name => "Rook") 

# works fine 
logger.debug(player.to_hash.to_json) 

# FAILS with the above error 
logger.debug(game.to_hash.to_json)  

ここで問題を引き起こしているコレクションのネストには何かが存在すると思われます。

アイデア?

class Game < Ohm::Model 
    collection :player, Player 

    def to_hash 
    super.merge(:players => players) 
    end 
end 


class Player < Ohm::Model 
    reference :game, Game 
    collection :pieces, Piece 

    attribute :name 

    def to_hash 
    super.merge(:name => name, :pieces => pieces) 
    end 
end 


class Piece < Ohm::Model 
    reference :player, Player 

    def to_hash 
    super.merge(:name => name) 
    end  
end 

答えて

2

私は、この問題を回避しますが見つかりました:

class Player < Ohm::Model 
    def to_hash 
    super.merge(:name => name, :pieces => pieces.all) 
    end 
end 
関連する問題