Can not call my associate table in my view

后端 未结 1 692
失恋的感觉
失恋的感觉 2021-01-29 08:10

Can not call my associate table in my view. have tried this. it is an application that only adds to the players. after the press \"start game\" and then he should come to the re

相关标签:
1条回答
  • 2021-01-29 08:27

    The CreateResults migration miss the player_id column.

    Do belongs_to :player in your Result class and correct the migration (or do another one).

    [Edit] I wasn't clear enough : I think you inverted the relationship logic. You actually did the "one result has several players". I suppose you want that a player has several results ?

    class Player
      has_many :results
    end
    
    class Result
      belongs_to :player
    end
    

    So you can do :

    @myplayer = Player.all.first
    @myplayer.results #it is an array of player's results
    @myplayerresult = @myplayer.results.first
    puts @myplayerresult.result
    

    If you want a one-to-one relationship, consider replacing has_many :results by has_one :result and so you can do @myplayer.result to get your result.

    0 讨论(0)
提交回复
热议问题