Mongoid - 如何使用嵌入式文档创建文档?
问题描述:
我目前正在使用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
答
您似乎在auction_track后缺少一个逗号:[]在您的参数。
什么auction_track后添加一个逗号:[]? – Rebitzele 2013-04-10 21:37:37
@Rebit谢谢,我不相信我错过了。无论如何,它似乎现在工作。 – Antimony 2013-04-10 21:51:09
当然可以!我只是把它写成官方的答案,所以人们会看到它。 – Rebitzele 2013-04-10 21:56:35