加入两个表的条件 - Ruby on Rails的 [英] Join two tables with condition - ruby on rails
本文介绍了加入两个表的条件 - Ruby on Rails的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想加入2表,游戏和game_logs。我做到了这一点:
game_joins = Game.joins(:game_logs)
它的工作。但事实是我唯一想加入其中player_id = 1(例如)。柱player_id只能在表game_logs找到。所以,当我这样做:
game_joins = Game.joins(:game_logs)。凡(:player_id => 1)
列player_id无法找到,因为
Game.joins(:game_logs)
将导致:
SELECT游戏。* FROM游戏
INNER JOIN game_logs ON game_logs.game_id = game.id
那么,问题是什么是可能我已经从表game_logs条件筛选game_joins。我希望我解释不够好。谢谢
解决方案
game_joins = Game.joins(:game_logs)。凡(:game_logs => {:player_id => 1 })
I wanna join 2 table, games and game_logs. I did it this:
game_joins = Game.joins(:game_logs)
It worked. But the thing is I only want to join where the player_id = 1(for example). Column player_id can only be found in table game_logs. So, when I do like this:
game_joins = Game.joins(:game_logs).where(:player_id => 1)
Column player_id can't be found, because
Game.joins(:game_logs)
will result :
SELECT games.* FROM games
INNER JOIN game_logs ON game_logs.game_id= game.id
So, the question is what is the possibility I have to filter game_joins with the condition from table game_logs. I hope I explain it well enough. Thanks
解决方案
game_joins = Game.joins(:game_logs).where(:game_logs => { :player_id => 1 })
这篇关于加入两个表的条件 - Ruby on Rails的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文