ruby-on-rails - 用于连接表的 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
解决方案
我实际上会为此使用实例方法。
# app/models/game_report.rb
def is_home_team?
self.team_id == self.game.home_team_id
end
true
如果报告与游戏的主队 id 具有相同的团队 id,否则将返回false
。
推荐阅读
- java - 带有 RemoteWebDriver 的 WebStorage
- rust - 如何从另一个具有固定自我的方法调用需要固定自我的方法?
- python - 使用 pip 安装 Django 但在 pip 列表中看不到
- java - 对许多可选参数进行 Spring 数据 JPA 查询的最佳实践是什么?
- c# - 从使用 DMZ API 的公共 Web API 获取文件
- c++ - 如何区分函数模板特化中的数组和指针
- android - 在应用关闭的情况下使用“react-native-camera”
- ionic-framework - 使用 cmd 使用选项卡启动新的离子应用程序不起作用
- azure - 如何为 Azure 标记生成警报
- ruby - 使用纯 Ruby 应用程序(无 Rails)撬动 Docker 容器不工作