首先,我还是ROR的新手,我试图想出更有效的方法来为我的数据表做数据库查询。Rails数据库查询ROR
我的模特协会
class Store < ActiveRecord::Base
has_many :surveys
has_many :customers
end
...
class Survey < ActiveRecord::Base
belongs_to :store
end
...
class Customer < ActiveRecord::Base
belongs_to :store
end
在我的DataTable
<tbody>
<% @store.surveys.each do |survey| %>
<tr class="th-container table-fixer">
<td><%= find_customer_id(survey.email).nil? ? survey.first_name : link_to(survey.first_name, store_customer_path(@store, find_customer_id(survey.email))) %></td>
<td><%= get_ltv(survey) %></td>
</tr>
<% end %>
</tbody>
find_customer_id和get_ltv方法如下
def find_customer_id(customer_email)
BwCustomer.find_by(email: customer_email)
end
与代码的问题是,目前我有超过1000我循环的活动记录对象,当find_customer_id方法被击中时,它会找到cus使用给定的电子邮件地址,并且查询需要15秒来处理。
在我的情况下,最好的方法是什么?
的解决方案,我虽然了解: 1.参加表格,让我没有打电话给另一台 2.延迟加载,仅在需要时
一些建议,将大大加载的对象赞赏
谢谢