使用外键显示用户名而不是用户ID

时间:2016-04-07 08:40:16

标签: mysql ruby-on-rails ruby

我有一个游戏列表,其中home_team_user_id和away_team_user_id都通过外键为其分配了user_id。

游戏只存储两个用户ID,但现在在游戏列表中我想显示用户名而不是user_id。

在视图中直接错误的溶解:

<%= User.find(game.home_team_user_id).email %>

这样可行但是如果找不到用户则会抛出错误。

我想将此逻辑移至控制器,但我似乎无法管理。

GAMES CONTROLLER:

class GamesController < ApplicationController
    before_action :authenticate_user!

    def index
        @games = Game.all
        @home_team_username = User.select(:id, :username).find(game.home_user)
        @away_team_username = User.select(:id, :username).find(game.away_user)
    end

模型:

class User < ActiveRecord::Base

    #RELATIONS SINGLE GAMES

     has_many :home_games,    class_name: 'Game', foreign_key: 'home_team_user_id'
     has_many :away_games, class_name: 'Game', foreign_key: 'away_team_user_id'
    end


class Game < ActiveRecord::Base

        belongs_to :home_user,    class_name: "User", foreign_key: "home_team_user_id"
        belongs_to :away_user, class_name: "User", foreign_key: "away_team_user_id"


end

查看:

<% @games.each do |game|%> 
                 <tbody> 
                     <td><%= game.created_at.strftime("%d-%m-%Y - %Hh%M")%></td>

            <% if game.home_score == game.away_score  %>
                     <td style="background-color: #e67300"><%= @home_team_username.username %></td>
                      <td style="background-color: #e67300"><%= game.home_score %></td>

                     <% elsif game.home_score > game.away_score  %>
                     <td style="background-color: #66ff33"><%= @home_team_username.username %></td>
                      <td style="background-color: #66ff33"><%= game.home_score %></td>

         <% elsif game.home_score < game.away_score %>

                     <td style="background-color: #ff1a1a"><%= @home_team_username.username %></td>
                        <td style="background-color: #ff1a1a"><%= game.home_score %></td>

         <% end %>
                     <td style="text-align: center">-</td>

            <% if game.home_score == game.away_score  %>
                        <td style="background-color: #e67300"><%= game.away_score %></td>
                        <td style="background-color: #e67300"><%= @away_team_username.username %></td>

         <% elsif game.home_score < game.away_score  %>
                        <td style="background-color: #66ff33"><%= game.away_score %></td>
                        <td style="background-color: #66ff33"><%= @away_team_username.username %></td>

         <% elsif game.home_score > game.away_score %>

                        <td style="background-color: #ff1a1a"><%= game.away_score %></td>
                     <td style="background-color: #ff1a1a"><%= @away_team_username.username %></td>

         <% end %>

我认为这应该有效,但它显示错误:

  

未定义的局部变量或方法`游戏&#39;对

     

由于某种原因,它无法在视图中使用循环中的游戏变量。

完成此任务的正确方法是什么?

3 个答案:

答案 0 :(得分:2)

您收到错误是因为您尝试在控制器中的索引操作中使用game,但此处未针对单个游戏定义此错误。在我看来,正确的方法就是在控制器中设置所有游戏:

def index
  @games = Game.all
end

然后在视图中获取名称,例如:

<td style="background-color: #e67300"><%= game.homeuser.username %></td>

显示已删除的用户&#39;你可以在循环的开头分配变量,如下所示:

home_user_name = if game.homeuser.present?
                   game.homeuser.username
                 else
                   'Deleted user'
                 end

或者在一行中:

home_user_name = game.homeuser.try(:username).presence || 'Deleted user'

然后使用它:

<td style="background-color: #e67300"><%= home_user_name %></td>

答案 1 :(得分:0)

你想在索引中展示什么?你想要所有游戏的所有用户吗?

@home_team_username = User.select(:id, :username).find(game.home_user)

可以替换为

@home_team_username = game.home_user

game.home_user是用户。然后,您可以返回该对象的用户名。

另外,我要将变量@home_team_username重命名为@home_team_user,或者存储username(不是用户)并将其传递给视图。

答案 2 :(得分:0)

好吧,你似乎在使用未声明的游戏变量(方法GamesController#index,第2行)。

相关问题