简体   繁体   English

Rails协会包括数组

[英]Rails Association includes array

I have a little problem about association in Rails. 我对Rails中的关联有一些疑问。

In fact I have a Class, Player which has_many Infraction which belongs_to SteamServer has_many InfractionTag which belongs_to Tag 其实我有一个类,它belongs_to的SteamServer的has_many InfractionTag这belongs_to的标签 播放器 ,其的has_many 违规

I would like get alle Infraction of a player, with the server, and all the tags of this infraction. 我想与服务器一起获取玩家的全部违规行为,以及该违规行为的所有标签。 I made: 我做了:

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

But I get: 但是我得到:

{: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 is nil, but when I launche this query in MySQL: :tags为零,但是当我在MySQL中启动此查询时:

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

I get a result. 我得到结果。

Here are my models: 这是我的模型:

Player 播放机

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

Infraction 侵害

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

InfractionTag InfractionTag

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

Tag 标签

class Tag < ActiveRecord::Base
    has_many :infraction_tags
end

SteamServer SteamServer

class SteamServer < ActiveRecord::Base
end

Someone would have any idea about how can I get all my tags with fewer requests ? 有人会对如何以更少的请求获得所有标签有任何想法吗? Thank you very much ! 非常感谢你 !

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

from .includes(:steam_server, :infraction_tags) 来自.includes(:steam_server, :infraction_tags)

to .includes(:steam_server, :tags) .includes(:steam_server, :tags)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM