我有以下内容:
order.rb
belongs_to :user
belongs_to :property
has_many :order_lines, :dependent => :destroy
order_line.rb
belongs_to :order
belongs_to :service
belongs_to :status, :class_name => "SettingsOption"
orders_controller.rb
def index
@orders = Order.active_w_property.find_all_by_user_id(current_user.id,
:include => {:order_lines => [:service, :status]})
end
index.html.erb
...
<%= render :partial => 'order', :collection => @orders %>
...
_order.html.erb
...
<% order.order_lines.each do |line| %>
<% total = total + line.price %>
<tr>
<td>
<%= line.service.option.nil? ?
t(line.service.name) :
[t(line.service.name), t(line.service.option)].join(" ") %>
</td>
<td><%= t(line.description) %></td>
<td class="align-right"><%= currencify_price(line.price) %></td>
<td class="align-center"><%= t(line.status.value) %></td>
</tr>
<% end %>
...
登录index.html.erb
←[4;36;1mOrder Load (2.0ms)←[0m ←[0;1mSELECT ""very long query""
←[4;35;1mOrderLine Load (1.0ms)←[0m ←[0mSELECT `order_lines`.* FROM `order_lines` WHERE (`order_lines`.order_id IN (10134,1
←[4;36;1mOrderLine Columns (5.0ms)←[0m ←[0;1mSHOW FIELDS FROM `order_lines`←[0m
←[4;35;1mService Columns (14.0ms)←[0m ←[0mSHOW FIELDS FROM `services`←[0m
←[4;36;1mService Load (3.0ms)←[0m ←[0;1mSELECT * FROM `services` WHERE (`services`.`id` IN (6,7,8,9,1,2,3,4,10,5)) ←[0m
←[4;35;1mSettingsOption Columns (4.0ms)←[0m ←[0mSHOW FIELDS FROM `settings_options`←[0m
←[4;36;1mSettingsOption Load (3.0ms)←[0m ←[0;1mSELECT * FROM `settings_options` WHERE (`settings_options`.`id` = 167) ←[0m
Rendering template within layouts/application
Rendering myimmonatie/orders/index
记录_order.html.erb
Rendered myimmonatie/orders/_order (8.0ms)
←[4;36;1mOrderLine Load (0.0ms)←[0m ←[0;1mSELECT * FROM `order_lines` WHERE (`order_lines`.order_id = 10112) ←[0m
←[4;35;1mCACHE (0.0ms)←[0m ←[0mSELECT * FROM `services` WHERE (`services`.`id` = 1) ←[0m
←[4;36;1mCACHE (0.0ms)←[0m ←[0;1mSELECT * FROM `settings_options` WHERE (`settings_options`.`id` = 167) ←[0m
←[4;35;1mCACHE (0.0ms)←[0m ←[0mSELECT * FROM `services` WHERE (`services`.`id` = 5) ←[0m
←[4;36;1mCACHE (0.0ms)←[0m ←[0;1mSELECT * FROM `settings_options` WHERE (`settings_options`.`id` = 167) ←[0m
←[4;35;1mCACHE (0.0ms)←[0m ←[0mSELECT * FROM `services` WHERE (`services`.`id` = 3) ←[0m
←[4;36;1mCACHE (0.0ms)←[0m ←[0;1mSELECT * FROM `settings_options` WHERE (`settings_options`.`id` = 167) ←[0m
←[4;35;1mCACHE (0.0ms)←[0m ←[0mSELECT * FROM `services` WHERE (`services`.`id` = 2) ←[0m
←[4;36;1mCACHE (0.0ms)←[0m ←[0;1mSELECT * FROM `settings_options` WHERE (`settings_options`.`id` = 167) ←[0m
对于控制器索引方法,所有包含的表都按预期加载。但是对于部分中的每个order_line,order_lines再次加载,尽管它们之前已加载。此外,每次都会查询服务(尽管来自缓存)。如何在没有新查询的情况下渲染所有内容?
更新:使用rails 2.3.11