每次加载页面时都会执行每个相关计算吗? [英] Is every relavant calculation performed every time the page is loaded?

查看:21
本文介绍了每次加载页面时都会执行每个相关计算吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型Wrapper",它有另一个模型Category"的_many,而另一个模型Category"又具有_many另一个模型Thing".

I have a model "Wrapper", which has_many of another model "Category", which in turn has_many of another model, "Thing".

"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

Category",然后,有这个实例方法,定义在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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆