无法在我的视图中调用我的关联表
无法在我的视图中调用我的关联表。 试过这个。 它是一个只会增加玩家的应用程序。 按下“开始游戏”然后他应该来到结果视图所有玩家的结果。 那么我当然会得到“玩家”表的名称,然后是“结果”表的绑定结果。 现在在quire,我只要在后台进入bidningen
视图:
控制器:
class ResultsController < ApplicationController def index @playersname = Player.all end
结束
模型:
class Result < ActiveRecord::Base # attr_accessible :title, :body has_many :players end
移民:
class CreateResults 40 t.string "cal", :limit => 40 t.string "sum",:limit => 300 t.timestamps end end
结束
CreateResults迁移错过了player_id列。
在您的Result类中执行belongs_to :player
并更正迁移(或执行另一个迁移)。
[编辑]我不够清楚:我认为你颠倒了关系逻辑。 你实际上做了“一个结果有几个球员”。 我想你想要一个玩家有几个结果?
class Player has_many :results end class Result belongs_to :player end
所以你可以这样做:
@myplayer = Player.all.first @myplayer.results #it is an array of player's results @myplayerresult = @myplayer.results.first puts @myplayerresult.result
如果你想要一对一的关系,可以考虑用has_one :result
替换has_many :results
,这样你就可以用@myplayer.result
来获得你的结果。