简体   繁体   English

红宝石按哈希哈希值分组

[英]ruby group by hashes' values in hash

sorry about the title, cannot express myself better. 对标题感到抱歉,无法更好地表达自己。

I have this: 我有这个:

{
  7758 => { 3259 => 10, 39625 => 10, 36410 => 20, 36238 => 20, 34951 => 20, 32101 => 10},
  7916 => { 3259 => 10, 39625 => 10, 36410 => 20, 36238 => 20, 34951 => 20, 32101 => 10},
  8857 => { 1000 => 10, 39625 => 10 }
}

the keys of those hashes represents record ids, the values represent the data that should go in their row attribute. 这些散列的键表示记录ID,值表示应在其row属性中输入的数据。

problem is that this hash can weigh a lot, so it's imprudent to issue an update on the database for each record represented in the hash. 问题在于该哈希值可能会占很大的比重,因此不宜在数据库中为哈希值中表示的每个记录发布更新。

instead I thought about grouping up hashes with identical values and have a structure for which I can just issue an update to records in one shot. 取而代之的是,我想到了将具有相同值的散列进行分组,并且具有一种结构,我可以针对该结构发布一次更新中的记录。

comparing hashes values can be done even by transforming the nested hashes themselves into a json string, since it's the datatype we use for that column. 甚至可以通过将嵌套的哈希自身转换为json字符串来比较哈希值,因为这是我们用于该列的数据类型。

in the end I'd like to issue an update_all for a series of variants that has the same hash content, I understand that the number of updates issues to the database is 1:1 to how unique are the hash values, but I kinda have the choice to sort their keys in someway before the comparison should we have something smart to compare existing values rather than converting the content to a string for comparison purposes. 最后,我想针对具有相同散列内容的一系列变体发布update_all ,我了解到数据库的更新问题数量与散列值的唯一性是1:1,但是我有点在比较之前选择以某种方式对它们的键进行排序的选择,应该是我们有一些聪明的方法来比较现有值,而不是将内容转换为字符串以进行比较。

what happens now is a normal update on each hash record in a cycle: 现在发生的是一个周期中每个哈希记录的正常更新:

UPDATE "table" SET "rates" = '{"3259":10,"39625":10,"36410":20,"36238":20,"34951":20,"32101":10}', WHERE "table"."variant_id" = 7758
UPDATE "table" SET "rates" = '{"3259":10,"39625":10,"36410":20,"36238":20,"34951":20,"32101":10}', WHERE "table"."variant_id" = 7916
UPDATE "table" SET "rates" = '{"1000":10,"39625":10}' WHERE "table"."variant_id" = 7916

I'd like to transform the original structure in something that allows me to perform this: 我想将原始结构转换为可以执行以下操作的内容:

UPDATE "table" SET "rates" = '{"3259":10,"39625":10,"36410":20,"36238":20,"34951":20,"32101":10}', WHERE "table"."variant_id" IN(7758, 7916)
UPDATE "table" SET "rates" = '{"1000":10,"39625":10}' WHERE "table"."variant_id" = 7916

I tried a 我尝试了

hash.group_by { |h| h[1].to_json }.each do |rate|

but I have this in rate : 但我有这个rate

["{\"3259\":10,\"39625\":10,\"36410\":20,\"36238\":20,\"34951\":20,\"32101\
":10}", [[7758, {3259=>10, 39625=>10, 36410=>20, 36238=>20, 34951=>20,
 32101=>10}], [7916, {3259=>10, 39625=>10, 36410=>20, 36238=>20, 
34951=>20, 32101=>10}], [8857, {3259=>10, 39625=>1...

Maybe something like this: 也许是这样的:

result = hash.each_with_object({}) do |(id, attributes), result|
  json_string = attributes.to_json
  result[json_string] ||= []
  result[json_string] << id
end

result.each do |json_string, ids|
  # ...
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM