私はルビーとレールを初めて使いました。後方への関連付けができません
私は現在3機種(ゲーム、マッチ、プレイヤー、ユーザー)
game.rbを持って
has_many :matches
match.rb
belongs_to :game
has_many :players
player.rb
belongs_to :match
belongs_to :user
今、私はthosを埋めたいプレイヤーモデルの関連付けを行う。
私は基本的に何をすべきか:
players_controller.rb
def create
@game = Game.find(params[:game_id])
@match = @game.matches.find(params[:match_id])
@player = @match.players.create(params[:player])
@player.user = User.find(current_user.id)
end
をその後私はビューで呼び出したい
<%= @player.user.email %>
は、このエラーが発生します
undefined method `email' for nil:NilClass
だから私の質問私は協会で後ろ向きに間違っているのですか?何とかできるはずです
あなたのuser.rbのアソシエーションは表示されませんが、「has_many:players」 – Chazu
のユーザー1:nのプレイヤー関係のように見えます。 user.rb 'has_many:players' – cschaeffler