Rails 关联包含数组

Rails Association includes array

我对 Rails 中的关联有一个小问题。

事实上我有一个 Class, Player which has_many Infraction which belongs_to SteamServer has_many InfractionTag 其中 belongs_to Tag

我想获取玩家的所有违规行为、服务器违规行为以及此违规行为的所有标签。我做了:

infractions = Infraction.where(player_steamid64: self.steamid64).includes(:steam_server, :infraction_tags);

但我得到:

{:infraction_type=>"Ban", :severite=>"Critique", :length=>"01d:00h:00m", :infraction_rp=>"No", :tags=>nil, :steam_server=>#<SteamServer id: 1, name: "Serveur Dev Pure System", ip: "188.165.198.224", ip_query: "188.165.198.224", port: 27015, begin_date: "2015-11-24 00:00:00", reputation: "0,100", reputation_rp: "0,100", accept_new: true, cache: false, validated: true, banner_460: nil, created_at: "2015-11-24 00:00:00", updated_at: "2015-11-24 00:00:00">, :created_at=>"24/11/2015 00:00"}

:tags 为零,但是当我在 MySQL 中启动此查询时:

SELECT `infraction_tags`.* FROM `infraction_tags` WHERE `infraction_tags`.`id` IN (1)

我得到一个结果。

这是我的模型:

玩家

class Player < ActiveRecord::Base
    has_many :infractions, class_name: 'Infraction', primary_key: 'steamid64', foreign_key: 'player_steamid64'
end

违规

class Infraction < ActiveRecord::Base
    has_many :infraction_tags
    belongs_to :player, class_name: 'Player', foreign_key: 'steamid64'
    belongs_to :steam_server, class_name: 'SteamServer', foreign_key: 'id'
end

违规标签

class InfractionTag < ActiveRecord::Base
    belongs_to :tag
    belongs_to :infraction
end

标签

class Tag < ActiveRecord::Base
    has_many :infraction_tags
end

SteamServer

class SteamServer < ActiveRecord::Base
end

有人会知道如何以更少的请求获得我的所有标签吗? 非常感谢!

class Infraction < ActiveRecord::Base
    has_many :infraction_tags
    has_many :tags, through: :infraction_tags
    belongs_to :player, class_name: 'Player', foreign_key: 'steamid64'
    belongs_to :steam_server, class_name: 'SteamServer', foreign_key: 'id'
end

来自 .includes(:steam_server, :infraction_tags)

.includes(:steam_server, :tags)