Ruby on Rails 如何处理“按类别从产品组中选择计数(*)”?

发布于 2024-09-15 10:41:20 字数 590 浏览 2 评论 0原文

假设有一个包含字段的表,

Products
--------
ID
CategoryID
Name
Price
  ... etc

Ruby on Rails 如何给出一个返回的表

select count(*) from products group by categoryID

,以显示每个类别中有多少个产品?与 Product 对象数组的 Products.find(:all) 相比,结果会如何?

作为更高级的操作,

select count(*) from products p inner join category c on p.categoryID = c.ID 
  group by categoryID

select average(price) from products p inner join category c on p.categoryID = c.ID 
  group by categoryID

怎么样?

Say if there is a table with fields

Products
--------
ID
CategoryID
Name
Price
  ... etc

How can Ruby on Rails give a table that returns

select count(*) from products group by categoryID

which is to show how many products in each category? How will the result be like, as opposed to Products.find(:all) which is an array of Product objects?

As a more advanced operation, how about

select count(*) from products p inner join category c on p.categoryID = c.ID 
  group by categoryID

and

select average(price) from products p inner join category c on p.categoryID = c.ID 
  group by categoryID

?

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

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

发布评论

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

评论(2

雪化雨蝶 2024-09-22 10:41:20

您可能想查看 ActiveRecord::Calculations 模块(我相信它可以完成您所要求的大部分操作):

http://api.rubyonrails.org/classes/ActiveRecord/Calculations.html

一些示例:

计数:

Product.group(:category_id).count

平均:

Product.joins(:category).group(:category_id).average(:price)

这些应该有望让您走上正确的道路。不过,一定要查看链接的文档,因为它非常有用。

You might want to check out the ActiveRecord::Calculations module (it does most of what you're asking, I believe):

http://api.rubyonrails.org/classes/ActiveRecord/Calculations.html

Some examples:

Count:

Product.group(:category_id).count

Average:

Product.joins(:category).group(:category_id).average(:price)

Those should hopefully get you on the right path. Definitely check out the linked documentation, though, as it's incredibly useful.

逆夏时光 2024-09-22 10:41:20

您是在问幕后发生了什么,还是只是想知道结果会是什么样子。如果是后者,那就学会爱上控制台吧!您可以轻松地自己找到答案:

$ script/console
Loading development environment (Rails 2.3.8)

>> Product.count
=> 229697

>> Product.count(:group => :category_id)
=> #<OrderedHash {27=>5588, 33=>41, 28=>156, 34=>22, 23=>15209, 1=>115357, 
     29=>109, 24=>68, 2=>14434, 25=>78576, 31=>85, 26=>4, 32=>48}>

如您所见,它为您提供了一个有序哈希映射category_ids 到这些类别的计数。

Are you asking what is happening behind the scenes or just what will the result look like. If it's the latter, then learn to love the console! You can easily find out for yourself:

$ script/console
Loading development environment (Rails 2.3.8)

>> Product.count
=> 229697

>> Product.count(:group => :category_id)
=> #<OrderedHash {27=>5588, 33=>41, 28=>156, 34=>22, 23=>15209, 1=>115357, 
     29=>109, 24=>68, 2=>14434, 25=>78576, 31=>85, 26=>4, 32=>48}>

As you can see, it gives you an ordered hash mapping category_ids to the counts for those categories.

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