Rails避免N + 1查询

时间:2017-05-11 03:02:07

标签: ruby-on-rails ruby activerecord

给出以下模型关系。

// webpack.config.js
module.exports = {
    ...
    plugins: [
        new webpack.ProvidePlugin({
           $: "jquery",
           jQuery: "jquery"
       })
    ]
};

这是我做的事情

给定组是一个组对象

group has_many :members
member has_many :missions
mission has_one :game_set_mission
mission has_one :game_set, :through => :game_set_mission

但它似乎发生# Add a new relationship in member.rb has_many :game_sets, :through => :missions # Get all game_set id group.members.map{ |gc| gc.game_sets.pluck(:id) }

如何改进我的代码?

1 个答案:

答案 0 :(得分:0)

希望能帮助你。

GameSet.includes(member: :group)
       .where('groups.id = ?', group.id)
       .pluck('game_sets.id')