我正在尝试在Active Record中对一系列记录进行分组,因此我可以进行一些计算来规范化每个记录的数量属性,例如:
用户输入日期和数量。 日期不是唯一的,因此每个日期我可能有10到20个数量。 我只需要处理每天的总数,而不是每个单独的记录。 因为在确定最高值和最低值之后,我将每一个转换为基本上除以n ,通常为10。
这就是我现在正在做的事情:
def heat_map(project, word_count, n_div) return "freezing" if word_count == 0 words = project.words counts = words.map(&:quantity) max = counts.max min = counts.min return "max" if word_count == max return "min" if word_count == min break_point = (max - min).to_f/n_div.to_f heat_index = (((word_count - min).to_f)/break_point).to_i end如果我显示一个包含所有单词计数的表格,那么这很有效,但我正在尝试将热图应用于显示每天运行总计的日历。 这显然不是总天数,所以我最终得到的数字超出正常规模。
在我进行标准化之前的一天,我无法想出一种方法来对单词计数进行分组并总计它们。 我尝试了一个group_by然后添加了map调用,但是我收到一个错误,一个未定义的方法错误。 有任何想法吗? 我也愿意采用更好/更清洁的方法来规范字数。
I'm trying to group a series of records in Active Record so I can do some calculations to normalize that quantity attribute of each record for example:
A user enters a date and a quantity. Dates are not unique, so I may have 10 - 20 quantities for each date. I need to work with only the totals for each day, not every individual record. Because then, after determining the highest and lowest value, I convert each one by basically dividing by n which is usually 10.
This is what I'm doing right now:
def heat_map(project, word_count, n_div) return "freezing" if word_count == 0 words = project.words counts = words.map(&:quantity) max = counts.max min = counts.min return "max" if word_count == max return "min" if word_count == min break_point = (max - min).to_f/n_div.to_f heat_index = (((word_count - min).to_f)/break_point).to_i endThis works great if I display a table of all the word counts, but I'm trying to apply the heat map to a calendar that displays running totals for each day. This obviously doesn't total the days, so I end up with numbers that are out of the normal scale.
I can't figure out a way to group the word counts and total them by day before I do the normalization. I tried doing a group_by and then adding the map call, but I got an error an undefined method error. Any ideas? I'm also open to better / cleaner ways of normalizing the word counts, too.
最满意答案
在不了解您的模型的情况下很难回答。 所以我假设你感兴趣的日期只是words表中的created_at日期。 我假设你的words表中有一个字段叫做word ,你可以存储实际的单词。
我还假设你可能在一天内有同一个单词的多个条目(可能有不同的数量)。
因此,这将为您提供每日字数的有序哈希:
project.words.group('DATE(created_at)').group('word').sum('quantity')如果这些猜测没有意义,那么也许你可以提供一些关于模型结构的更多细节。
Hard to answer without knowing a bit more about your models. So I'm going to assume that the date you're interested in is just the created_at date in the words table. I'm assuming that you have a field in your words table called word where you store the actual word.
I'm also assuming that you might have multiple entries for the same word (possibly with different quantities) in the one day.
So, this will give you an ordered hash of counts of words per day:
project.words.group('DATE(created_at)').group('word').sum('quantity')If those guesses make no sense, then perhaps you can give a bit more detail about the structure of your models.
更多推荐
发布评论