不明白为什么“preload / eager_load”在ROR中不起作用?

时间:2017-01-10 09:53:42

标签: ruby-on-rails

我不明白为什么“域名”被多次查询,虽然我在控制器中使用eager_load(如服务器,但“服务器”只被查询一次)

代码:

在控制器中:

       @problems = Problem.all.eager_load(:serverity, :domain, :nation,  :status,:urgency,:user, :works).order(start_time: :desc)
模特中的

    class Problem < ActiveRecord::Base
     belongs_to :serverity
     belongs_to :urgency
     belongs_to :nation
     belongs_to :status
     belongs_to :user  
     belongs_to :cause
     belongs_to :domain
     has_many   :works
     has_many   :attachments, :dependent => :destroy   
   end

在视图中:

<% @problems.each do |problem| %>
       <td>
    <%= link_to problem.name, problem, :method => :get %></td>
    <td>
      <% if problem.domain.root? %>
      <%= problem.domain.name %>
      <%else%>
      <%= problem.domain.parent.name %>
      <%end%>
    </td> 
    <td><%= problem.serverity.name %></td>
<%>

在日志中:

  Processing by ProblemsController#index as HTML                                                                           
  [1m[36mUser Load (15.6ms)[0m  [1m[34mSELECT  `users`.* FROM `users` WHERE          `users`.`id` = 1 LIMIT 1[0m              
  [1m[36mCACHE (0.0ms)[0m  [1m[34mSELECT  `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1[0m  [["id", 1], ["LIM
  Rendering problems/index.html.erb within layouts/application                                                             
  [1m[35m (78.0ms)[0m  [1m[34mSELECT COUNT(DISTINCT `problems`.`id`) FROM `problems` LEFT OUTER JOIN `serverities` ON 
  [1m[35m (0.0ms)[0m  [1m[34mSELECT COUNT(DISTINCT `problems`.`id`) FROM `problems` LEFT OUTER JOIN `serverities` ON `
  [1m[35m (0.0ms)[0m  [1m[34mSELECT COUNT(DISTINCT `problems`.`id`) FROM `problems` LEFT OUTER JOIN `serverities` ON `
  [1m[35m (0.0ms)[0m  [1m[34mSELECT COUNT(DISTINCT `problems`.`id`) FROM `problems` LEFT OUTER JOIN `serverities` ON `
  [1m[35m (0.0ms)[0m  [1m[34mSELECT COUNT(DISTINCT `problems`.`id`) FROM `problems` LEFT OUTER JOIN `serverities` ON `
  [1m[35m (0.0ms)[0m  [1m[34mSELECT COUNT(DISTINCT `problems`.`id`) FROM `problems` LEFT OUTER JOIN `serverities` ON `
  [1m[35mSQL (31.2ms)[0m  [1m[34mSELECT `problems`.`id` AS t0_r0, `problems`.`name` AS t0_r1, `problems`.`start_time` 
  [1m[36mDomain Load (0.0ms)[0m  [1m[34mSELECT  `domains`.* FROM `domains` WHERE `domains`.`id` = 3 LIMIT 1[0m       
  [1m[35m (0.0ms)[0m  [1m[34mSELECT COUNT(*) FROM `works` WHERE `works`.`problem_id` = 238 AND `works`.`status` = 'hoà
  [1m[36mDomain Load (0.0ms)[0m  [1m[34mSELECT  `domains`.* FROM `domains` WHERE `domains`.`id` = 6 LIMIT 1[0m       
  [1m[36mCACHE (0.0ms)[0m  [1m[34mSELECT  `domains`.* FROM `domains` WHERE `domains`.`id` = 6 LIMIT 1[0m  [["id", 6],
  [1m[35m (0.0ms)[0m  [1m[34mSELECT COUNT(*) FROM `works` WHERE `works`.`problem_id` = 239 AND `works`.`status` = 'hoà
  [1m[36mCACHE (0.0ms)[0m  [1m[34mSELECT  `domains`.* FROM `domains` WHERE `domains`.`id` = 6 LIMIT 1[0m  [["id", 6],
  [1m[36mCACHE (0.0ms)[0m  [1m[34mSELECT  `domains`.* FROM `domains` WHERE `domains`.`id` = 6 LIMIT 1[0m  [["id", 6],
  [1m[36mCACHE (0.0ms)[0m  [1m[34mSELECT  `domains`.* FROM `domains` WHERE `domains`.`id` = 6 LIMIT 1[0m  [["id", 6],
  [1m[36mCACHE (0.0ms)[0m  [1m[34mSELECT  `domains`.* FROM `domains` WHERE `domains`.`id` = 6 LIMIT 1[0m  [["id", 6],
  [1m[36mCACHE (0.0ms)[0m  [1m[34mSELECT  `domains`.* FROM `domains` WHERE `domains`.`id` = 6 LIMIT 1[0m  [["id", 6],
  [1m[36mCACHE (0.0ms)[0m  [1m[34mSELECT  `domains`.* FROM `domains` WHERE `domains`.`id` = 6 LIMIT 1[0m  [["id", 6],

0 个答案:

没有答案