首页 > 解决方案 > 用于连接表的 Rails 查询

问题描述

我有 2 张桌子。游戏和游戏报告

游戏表有字段:id、home_team_id、away_team_id、home_club_id、away_club_id 等

game_report 有字段:id、game_id、report_type、team_id 等

game_report 表中的 team_id 是游戏表中的 home_team_id 或 away_team_id。

我需要一个查询来检查游戏表中的 home_team_id 和 away_team_id 是否具有相同的 report_type

标签: ruby-on-rails

解决方案


我实际上会为此使用实例方法。

# app/models/game_report.rb
def is_home_team?
  self.team_id == self.game.home_team_id
end

true如果报告与游戏的主队 id 具有相同的团队 id,否则将返回false


推荐阅读