【问题标题】:Ruby on Rails : complex calculation queryRuby on Rails:复杂的计算查询
【发布时间】:2011-12-19 11:15:00
【问题描述】:

在 ruby​​ on rails 应用程序中,我有一个名为 test_details 的表,其中包含以下字段:

id,
profileId,
testName,
testId,
dateCovered,
levelCompleted,
userResult,
unit

这里levelCompleted 可以有五分之一的值:

l1, l2, l3, l4, l5

我想创建一个报告以根据 testName 及其百分比来查找每个级别的计数

像这样:

testID  ll       l2          l3        l4       l5
-------------------------------------------------------
test1   25(25%)  45(35.2%)   12(10%)   12(10%)  15(12%)
test2   25(25%)  45(35.2%)   12(10%)   12(10%)  15(12%)
test3   58(25%)  445(35.2%)  145(10%)  42(10%)  25(12%)

(数值和百分比不是精确值)

如何在控制器和视图中执行此操作?

我的控制器代码:

  @all_tests = TestDetails.count
  @test_details  = TestDetails.select("DISTINCT(testName)")
  puts(@test_details)
  @test_details.each do | test_details | 
    @test_name = test_details["testName"];
    @level1_total  = TestDetails.where("testName =? and levelCompleted =? and dateCovered is ?",test_details["testName"],1,nil).count
    @level1_total_percent = (@level1_total/@all_tests.to_f)*100

    @level2_total  = TestDetails.where("testName =? and levelCompleted =? and dateCovered is ?",test_details["testName"],2,nil).count
    @level2_total_percent = (@level2_total/@all_tests.to_f)*100

    @level3_total  = TestDetails.where("testName =? and levelCompleted =? and dateCovered is ?",test_details["testName"],3,nil).count
    @level3_total_percent = (@level3_total/@all_tests.to_f)*100

    @level4_total  = TestDetails.where("testName =? and levelCompleted =? and dateCovered is ?",test_details["testName"],4,nil).count
    @level4_total_percent = (@level4_total/@all_tests.to_f)*100

    @level5_total  = TestDetails.where("testName =? and levelCompleted =? and dateCovered is ?",test_details["testName"],5,nil).count
    @level5_total_percent = (@level5_total/@all_tests.to_f)*100

结束

【问题讨论】:

  • @m_x 我想要一个类似于上面显示的表结构的视图..
  • 好的,我下班后试试。铜
  • @m_x 好的……谢谢……我也在尝试一些工作……
  • @m_x ,我试过了,但还没有解决方案...
  • 回答并编辑了您的帖子以使其更清晰。

标签: ruby-on-rails ruby-on-rails-3 model-view-controller


【解决方案1】:

首先,您查询数据库的方式完全是低效的。您正在进行大量 (2 + 5 * number of distinct testNames) 查询以使其正常工作,每个查询都扫描整个表!在一张大桌子上,您的数据库肯定会为此窒息。

我强烈建议您详细了解 SQL、数据库的工作原理以及如何以正确的方式设计数据库。The Wikipedia entry for db design is a good start,然后您可以前往something more consistent

在原始 SQL 中,您尝试实现的目标可能在一个简单的查询中

SELECT      testName, levelCompleted, COUNT(*) AS count
  FROM      test_details
  GROUP BY  testName, levelCompleted

# BTW, your field names don't follow rails convention...
# try to follow the conventions unless you have
# very good reasons not to do so.

这将获得完成计算所需的所有数据,如下所示:

testName | levelCompleted  | count
-----------------------------------
testone  |       1         |  10
testone  |       2         |  10
testone  |       3         |  15
testone  |       4         |  3
testone  |       5         |  2
testTWO  |       1         |  10
testTWO  |       2         |  15
testTWO  |       3         |  4

# and so on... there's two gotcha though: 
# - if there is no record for a particular 
#   level and testName, itwon't appear at all in this !
# - the order of the rows is not guaranteed, 
#   but you can enforce it with an ORDER clause

也就是说,在 Rails 中计算可以证明自己有点棘手。我没有足够的时间在这里开发这个,我认为给你一个开箱即用的答案也对你没有帮助......你真的应该深入研究 RoR 指南,尤其是关于 migrations , associations, 和 queries

不过,如果您有任何问题,我仍然可以联系。

【讨论】:

  • 另一件事:避免使用像Test 这样的通用名称来命名您的模型。这是与其他模块甚至 Rails 的本机部分产生冲突的最佳方式。
  • thanx @m_x ,我是 ruby​​ 的新手,我开始研究 ror 才两周。感谢您的宝贵建议。我想知道如何从这个查询中获取百分比数字..
【解决方案2】:

不要指望这里的工作程序形式的现成答案。你不会得到它。相反,请期待有关如何实施和学习的建议。

为什么不尝试使用散列而不是多个数组呢? Thisthis 是其中的一些参考资料。

哈希的结构可能如下所示:

report = {
  test1 => {
    level1 => {
      total => "total", 
      percent => "percent"
    },

    level2 => {
      total => "total",
      percent => "percent"
    },

    ...    
  },

  test2 => {
    level1 => {
      total => "total",
      percent => "percent"
    }

    ...
  }
}

如果遇到困难,为什么不尝试这种方法并返回一些代码?我们会尽力帮助您重构和优化。

【讨论】:

  • 其实我的目的是从这些数据中创建pdf..目前正在使用prawn来实现。我想知道这个hash数据是否可以在.prawn页面中使用来生成pdf?跨度>
  • 我不能动态生成哈希?有什么例子吗?
  • 你尝试了什么?你为什么不用代码更新你的问题?
  • 我对哈希一无所知..我不知道如何开始。
  • 所以,您在我的回答中没有浏览这些链接。为什么不在这里学习 Ruby 哈希:rubymonk.com/chapters/10-hashes-in-ruby/lessons/…。说真的,这会有所帮助。
猜你喜欢
  • 1970-01-01
  • 2012-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多