.在ActiveRecord中查找方法

发布于 2024-09-18 12:04:02 字数 472 浏览 4 评论 0原文

我正在使用 ActiveRecord 和 Ruby(Rails 之外)来跟踪一些统计数据。

我向其中一个模型添加了一个方法 (total_cost),以使用当前模型中的几列以及另一个模型中的一列进行计算。

我真的很希望能够使用 ActiveRecord 的一些数学规定(求平均值、求和)以及使用我定义的方法进行简单的旧发现,但是任何这样做的尝试都会导致我(完全可以理解)“未知列”错误,例如

Article.find(:all, :conditions => ["total_cost > ?", 300])

结果,我正在以一种我们可以描述为暴力的方式做事,只需找到所有文章,然后将每个文章的total_value 填充到一个数组中,然后对其进行求和和平均值。

除了我正在做的事情之外,我还有其他选择吗?我是否应该通过 ActiveRecord 查看 MySQL 本身来计算有问题的值?

I'm using ActiveRecord and Ruby (outside of Rails) to track some statistics.

I added a method (total_cost) to one of my models to do calculations using a couple of the columns in the current model as well as a column from another model.

I'd really like to be able to use some of ActiveRecord's provisions for math (averaging, sums) and plain old finding using the method I defined, but any attempt to do so nets me (a perfectly understandable) 'Unknown column' error, e.g.

Article.find(:all, :conditions => ["total_cost > ?", 300])

As a result, I'm doing things in a manner we could describe as brute-forcey, just finding all the Articles then stuffing each one's total_value into an array and doing sums and averages with that.

Do I have any alternatives besides what I'm doing? Should I be looking past ActiveRecord to MySQL itself to calculate the values in question?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清浅ˋ旧时光 2024-09-25 12:04:02

你可以做

Article.sum(:total_cost, :conditions => ["total_cost > ?", 300])
Article.average(:total_cost, :conditions => ["total_cost > ?", 300])

给定total_cost是一个方法:

total_cost = 'freelance_cost + effort * editor.hourly_rate'
Artice.sum(total_cost, :conditions => ["#{total_cost} > ?", 300], :joins => [:editor])

You can do

Article.sum(:total_cost, :conditions => ["total_cost > ?", 300])
Article.average(:total_cost, :conditions => ["total_cost > ?", 300])

Given total_cost is a method:

total_cost = 'freelance_cost + effort * editor.hourly_rate'
Artice.sum(total_cost, :conditions => ["#{total_cost} > ?", 300], :joins => [:editor])
云淡风轻 2024-09-25 12:04:02

一种选择是将总成本的计算方式转换为 SQL 表达式,以便您可以将该表达式替换为查询中可能需要的“total_cost”的任何引用。如果总成本计算起来很复杂或者取决于多个表格,那么这可能会非常麻烦。

如果可行的话,在您的 Article 模型上设置 total_cost 属性听起来确实有意义。您可以在模型更改时使用回调进行计算,然后保存,而不是在有人调用方法 a.total_cost 时进行计算。例如:

def before_save
  self.total_cost = ... whatever ...
end

这将允许您使用总和并平均总成本。这比获取文章、计算每篇文章的总成本然后汇总它们要高效得多。它可能也比每次请求时计算它更有效。

该解决方案取决于总成本值不会发生太大变化。如果它根据各种不同对象的变化而一直变化,那么检测总成本何时必须变化将会更加困难,并且以这种方式计算它的效率会降低。但这肯定会让你的计算变得更容易。

One option is to translate the way that total cost is calculated into an SQL expression so that you can then substitute that expression for any reference you might need to 'total_cost' in a query. That could potentially be quite cumbersome if total cost is complicated to calculate or depends on several tables.

If it is feasible it does sound like it makes sense to have a total_cost attribute on your Article model. Instead of having the calculation take place when someone calls the method a.total_cost you could calculate it when the model changes using a callback and save it then. For example:

def before_save
  self.total_cost = ... whatever ...
end

That would allow you to use the sum and average the total costs. It would be much more efficient than getting articles, calculating the total cost for each and then aggregating them. It would probably also be more efficient than calculating it each time it is requested.

That solution is dependent on the value for total cost not changing a lot. If it changes all the time based on various different objects changing then it will be more awkward to detect when total cost must change and less efficient to calculate it in this way. It would certainly make your calculations easier though.

維他命╮ 2024-09-25 12:04:02

find 方法创建 SQL。您不能在其中使用 total_cost 方法。所以需要用SQL编写total_cost的计算。如果您想使用该方法,您可以从所有数据中过滤必要的数据。

@articles = Article.find :all
@articles = @articles.select {|a| a.total_cost > 300}

但这种方式花费更多的资源..

find method creates SQL. You can't use total_cost method in it. So it needs to write the calculation of total_cost in SQL. If you want to use the method, you can filter necessary data from all.

@articles = Article.find :all
@articles = @articles.select {|a| a.total_cost > 300}

But this way spends more resources..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文