我正在尝试在我的关联表中放置一个随机 ID 号。我该怎么办?
def create
@rand = rand(1...10)
@player = Player.new(params[:player])
@player.result_id = @rand
if @player.save
render :action => "new"
else
render :action => "new"
end
end
我正在尝试在我的关联表中放置一个随机 ID 号。我该怎么办?
def create
@rand = rand(1...10)
@player = Player.new(params[:player])
@player.result_id = @rand
if @player.save
render :action => "new"
else
render :action => "new"
end
end
只需选择一个随机结果 id:
@player.result_id = Result.offset(rand(Result.count)).select('id').first.id
关于选择随机记录,请参阅 Rails 3:获取随机记录