2011-01-18 4 views
2

나는 옴 모델 트리를 가지고 있습니다. 게임에는 플레이어가 있고, 플레이어에는 조각이 있습니다. 전체 내용은 다음과 같습니다. 내가 JSON과 같은 구조를 렌더링하는 데 갈 때 기본적으로, 내가 인코딩 오류를 참조하십시오옴 모델 트리를 JSON 구조로 변환

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

그러나, 나는 출력 할 수있는 플레이어와 에러없이 자신의 조각.

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 Player < Ohm::Model 
    def to_hash 
    super.merge(:name => name, :pieces => pieces.all) 
    end 
end 
:
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

나는이 문제를 해결 작동 발견