每次加载页面时都会执行所有相关的计算吗? [英] Is every relavant calculation performed every time the page is loaded?
问题描述
我有一个模型"Wrapper",它具有另一个模型"Category"的种类,而该模型又具有另一个模型"Thing"的种类.
I have a model "Wrapper", which has_many of another model "Category", which in turn has_many of another model, "Thing".
事物"具有整数属性:count
和:number
.它还具有models/thing.rb
中定义的实例方法:
"Thing" has the integer attributes :count
and :number
. It also has an instance method defined as such in models/thing.rb
:
def ratio
(self.count + self.number).to_f / Thing.all.count.to_f
end
然后,
类别"具有在models/category.rb
中定义的此实例方法:
"Category", then, has this instance method, defined in models/category.rb
:
def thing_ratios
self.things.sum(&:ratio.to_f)
end
最后,我的wrapper.html.erb
视图显示类别,按thing_ratios
的顺序列出:
Finally, my wrapper.html.erb
view shows Categories, listed in order of thing_ratios
:
<%= @wrapper.categories.all.order(&:thing_ratios).each do |category| %>
...
我的问题是这样的:每当有人重新加载页面wrapper.html.erb
时,是否需要重新计算每个相关的计算,直到与页面上每个类别相关的每件事都降到self.count
?
My question is this: every time someone reloads the page wrapper.html.erb
, would every single relavant calculation have to be recalculated, all the way down to self.count
for every Thing associated with every Category on the page?
推荐答案
除了@Kelseydh提供的资源外,您还可以考虑在同一请求中多次击中同一函数时的记忆.但是,在处理请求后,它将不会保留其值.
In addition to the resources that @Kelseydh provided, you can also consider memoization for when you hit the same function multiple times as part of the same request. However, it will not retain its value after the request is processed.
这篇关于每次加载页面时都会执行所有相关的计算吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!