2013-04-10 83 views
0

我目前正在使用Mongoid開發一個Rails項目。我定義了一個Game模式,嵌入了許多GamePlayers。不幸的是,我無法弄清楚如何製作新遊戲。我可以創建沒有球員使用Game.create一場比賽,但是當我嘗試創建玩家在遊戲中,也給出了一個語法錯誤。我嘗試過在線搜索,但是我一直無法找到與問題相關的任何內容。Mongoid - 如何使用嵌入式文檔創建文檔?

這裏是在GamesController我attemped創建代碼。

def new 
    @game = Game.create(
     epoch: 1,  
     turn: 0, 
     auction_turn: -1, 
     auction_type: -1, 
     sun: 1, 
     ras: 0, 
     auction_track: [] 

     game_players: [ #doesn't work 
      { suns:[9,6,5,2] 
      }   
     ]  
    ) 

    redirect_to :action => "show", :id => @game._id 
    end 

將會產生錯誤

/home/<redacted>/Ra/ra_server/app/controllers/games_controller.rb:36: syntax error, unexpected tIDENTIFIER, expecting ')' 
     game_players: [ #doesn't work 

這裏是我的模型

class Game 
    include Mongoid::Document 

    field :epoch, type:Integer 
    field :turn, type:Integer 
    field :auction_turn, type:Integer 
    field :auction_type, type:Integer 
    field :sun, type:Integer 
    field :ras, type:Integer 
    field :auction_track, type:Array 


    embeds_many :game_players 
end 

class GamePlayer 
    include Mongoid::Document 

    field :score, type:Integer 
    field :bid, type:Integer 
    field :suns, type:Array 
    field :next_suns, type:Array 
    field :pharaohs, type:Integer 
    field :niles, type:Integer 
    field :floods, type:Integer 
    field :gods, type:Integer 
    field :gold, type:Integer 
    field :civilizations, type:Array 
    field :monuments, type:Array 

    embedded_in :game 
end 
+0

什麼auction_track後添加一個逗號:[]? – Rebitzele 2013-04-10 21:37:37

+0

@Rebit謝謝,我不相信我錯過了。無論如何,它似乎現在工作。 – Antimony 2013-04-10 21:51:09

+0

當然可以!我只是把它寫成官方的答案,所以人們會看到它。 – Rebitzele 2013-04-10 21:56:35

回答

1

您似乎在auction_track後缺少一個逗號:[]在您的參數。