N + 1查询问题

时间:2015-07-14 04:57:46

标签: ruby-on-rails-4 activerecord

我无法理解为什么bullect gem在我将查询实际包含在查询中时会抱怨n + 1个查询

问题

检测到N + 1个查询

AddonTypeValue => [:addon_option_values]

Add to your finder: :includes => [:addon_option_values]

查询

addon_types = AddonType.includes(addon_type_values:
      [addon_option_values: :addon_option_type]).
      where(addon_type_values: {published: true, design_id: self.id}).
      where{addon_type.addon_type_values.visible_for != not_for}

我的观点 - jbuilder

json.addon_option_values addon_type_value.addon_option_values do |aov|

    json.id aov.id

    json.p_name aov.p_name

    json.position aov.position

end

请求输出日志 - N + 1查询方法调用堆栈

/app/views/api/v1/designs/show.json.jbuilder:45:在_app_views_api_v__designs_show_json_jbuilder ___ 3899644929766612648_11335760'中的块(2级)

/app/views/api/v1/designs/show.json.jbuilder:39:in在_app_views_api_v__designs_show_json_jbuilder ___ 3899644929766612648_11335760'中的块

/app/views/api/v1/designs/show.json.jbuilder:35:in`_app_views_api_v__designs_show_json_jbuilder ___ 3899644929766612648_11335760'

/app/views/api/v1/designs/show.json.jbuilder:45:在_app_views_api_v__designs_show_json_jbuilder ___ 3899644929766612648_11335760'中的块(2级)

/app/views/api/v1/designs/show.json.jbuilder:39:in在_app_views_api_v__designs_show_json_jbuilder ___ 3899644929766612648_11335760'中的块

/app/views/api/v1/designs/show.json.jbuilder:35:in`_app_views_api_v__designs_show_json_jbuilder ___ 3899644929766612648_11335760'

0 个答案:

没有答案