首先查询:
>> Player.all
#<ActiveRecord::Relation [
#<Player id: 1, first_name: "Daniel", last_name: "Margush", aleague_club: "AU", position: "GLK", created_at: "2018-03-16 10:21:08", updated_at: "2018-03-16 10:21:08">,
#<Player id: 2, first_name: "Michael", last_name: "Marrone", aleague_club: "AU", position: "DEF", created_at: "2018-03-16 10:21:08", updated_at: "2018-03-16 10:21:08">,
#<Player id: 3, first_name: "Ben", last_name: "Warland", aleague_club: "AU", position: "DEF", created_at: "2018-03-16 10:21:08", updated_at: "2018-03-16 10:21:08">,
#<Player id: 4, first_name: "Taylor", last_name: "Regan", aleague_club: "AU", position: "DEF", created_at: "2018-03-16 10:21:08", updated_at: "2018-03-16 10:21:08">,
#<Player id: 5, first_name: "Vince", last_name: "Lia", aleague_club: "AU", position: "DMD", created_at: "2018-03-16 10:21:08", updated_at: "2018-03-16 10:21:08">,
#<Player id: 6, first_name: "Ryan", last_name: "Kitto", aleague_club: "AU", position: "AMD", created_at: "2018-03-16 10:21:08", updated_at: "2018-03-16 10:21:08">,
#<Player id: 7, first_name: "", last_name: "Isaias", aleague_club: "AU", position: "DMD", created_at: "2018-03-16 10:21:08", updated_at: "2018-03-16 10:21:08">,
#<Player id: 8, first_name: "Baba", last_name: "Diawara", aleague_club: "AU", position: "STK", created_at: "2018-03-16 10:21:08", updated_at: "2018-03-16 10:21:08">,
#<Player id: 9, first_name: "Karim", last_name: "Matmour", aleague_club: "AU", position: "WNG", created_at: "2018-03-16 10:21:08", updated_at: "2018-03-16 10:21:08">,
#<Player id: 10, first_name: "Johan", last_name: "Absalonsen", aleague_club: "AU", position: "WNG", created_at: "2018-03-16 10:21:08", updated_at: "2018-03-16 10:21:08">, ...]>
第二次查询:
>> PlayerTransaction.where(league_id: 1).select("DISTINCT ON(player_id) player_id, updated_at, transaction_type, value, club_id").order("player_id, updated_at DESC")
#<ActiveRecord::Relation [
#<PlayerTransaction id: nil, transaction_type: nil, player_id: "1", value: 313387, club_id: 8, updated_at: "2018-03-16 10:21:08">,
#<PlayerTransaction id: nil, transaction_type: nil, player_id: "2", value: 623542, club_id: 4, updated_at: "2018-03-16 10:21:08">,
#<PlayerTransaction id: nil, transaction_type: nil, player_id: "4", value: 488987, club_id: 5, updated_at: "2018-03-16 10:21:08">,
#<PlayerTransaction id: nil, transaction_type: nil, player_id: "6", value: 345036, club_id: 5, updated_at: "2018-03-16 10:21:08">,
#<PlayerTransaction id: nil, transaction_type: nil, player_id: "8", value: 830195, club_id: 7, updated_at: "2018-03-16 10:21:08">,....
我想将PlayerTransaction查询的结果合并到Player查询的结果中,因此,基于PlayerTransaction.player_id和Player.id键的比较,我留下了类似的内容:
#<Player id: 1, first_name: "Daniel", last_name: "Margush", aleague_club: "AU", position: "GLK", created_at: "2018-03-16 10:21:08", value: 313387, club_id: 8, updated_at: "2018-03-16 10:21:08">,
#<Player id: 2, first_name: "Michael", last_name: "Marrone", aleague_club: "AU", position: "DEF", created_at: "2018-03-16 10:21:08", value: 623542, club_id: 4, updated_at: "2018-03-16 10:21:08">,
#<Player id: 3, first_name: "Ben", last_name: "Warland", aleague_club: "AU", position: "DEF", created_at: "2018-03-16 10:21:08", updated_at: "2018-03-16 10:21:08">,
#<Player id: 4, first_name: "Taylor", last_name: "Regan", aleague_club: "AU", position: "DEF", created_at: "2018-03-16 10:21:08", value: 488987, club_id: 5, updated_at: "2018-03-16 10:21:08">,
#<Player id: 5, first_name: "Vince", last_name: "Lia", aleague_club: "AU", position: "DMD", created_at: "2018-03-16 10:21:08", updated_at: "2018-03-16 10:21:08">,...
正如您所见,ID为1,2,4的玩家拥有额外的值。
答案 0 :(得分:0)
您应该执行以下操作:
Player.joins(:player_transactions).select("players.id, player_transactions.update_at, player_transactions.transaction_type, player_transactions.value, player_transactions.club_id").where(league_id: 1)