Rails哈希值由它们组合

时间:2016-09-07 03:43:42

标签: ruby-on-rails ruby

我有一个questions列表,我需要将它们分开。关系是

Question_set has_many questions
BookVolume has_many questions
Subject has_many  book_volumes
Publisher has_many subjects
Section has_many :questions

现在我只将questions及其相对模型idname放入hash内的array

data = []
question_set.questions.each do |q|
    data << {publisher: {id: q.publisher.id, name: q.publisher.name}, subject: {id: q.book_volume.subject.id, name: q.book_volume.subject.name}, volume: {id: q.book_volume_id, name: q.book_volume.name}, chapter: [{id: q.section_id, name: q.section.name}]}  
end

因此,data基本上将是

>>data

[
    {
        :publisher => {
              :id => 96,
            :name => "P1"
        },
          :subject => {
              :id => 233,
            :name => "S1"
        },
           :volume => {
              :id => 1136,
            :name => "V1"
        },
          :chapter => [
            {
                  :id => 16155,
                :name => "C1"
            }
        ]
    },
       {
        :publisher => {
              :id => 96,
            :name => "P1"
        },
          :subject => {
              :id => 233,
            :name => "S1"
        },
           :volume => {
              :id => 1136,
            :name => "V1"
        },
          :chapter => [
            {
                  :id => 16158,
                :name => "C2"
            }
        ]
    }
]

但是,如果chapterpublishersubject相同,我希望合并volume 所以,在这种情况下,它将是

>>data 

[
    {
        :publisher => {
              :id => 96,
            :name => "P1"
        },
          :subject => {
              :id => 233,
            :name => "S1"
        },
           :volume => {
              :id => 1136,
            :name => "V1"
        },
          :chapter => [
            {
                  :id => 16155,
                :name => "C2"
            },
            {
                  :id => 16158,
                :name => "C2"
            }
        ]
    }
]

2 个答案:

答案 0 :(得分:2)

<强>代码

def group_em(data)
  data.group_by { |h| [h[:publisher], h[:subject], h[:volume]] }.
       map do |k,v|
         h = { publisher: k[0], subject: k[1], volume: k[2] }
         h.update(chapters: v.each_with_object([]) { |f,a|
           a << f[:chapter] }.flatten)
       end
end

示例

data等于哈希数组(上面的第一个数组)。

group_em(data)
  #=> [{:publisher=>{:id=>96, :name=>"P1"},
  #     :subject=>{:id=>233, :name=>"S1"},
  #     :volume=>{:id=>1136, :name=>"V1"},
  #     :chapters=>[{:id=>16155, :name=>"C1"}, {:id=>16158, :name=>"C2"}]
  #    } 
  #   ] 

此处data仅包含两个哈希值,并且这些哈希值对于键:publisher:subject:volume具有相同的值。此代码允许数组具有任意数量的哈希值,并将按这三个键的值数组对它们进行分组,从而为每个组生成一个哈希值。此外,键:chapters的值是包含单个哈希的数组,但此代码允许该数组包含多个哈希。 (如果该数组总是只有一个哈希值,请考虑将:chapters的值设置为哈希本身,而不是包含该哈希值的数组。)

<强>解释

请参阅Enumerable#group_byHash#update(又名Hash#merge!)。

步骤如下。

h = data.group_by { |h| [h[:publisher], h[:subject], h[:volume]] }
  #=> {
  #    [{:id=>96, :name=>"P1"},
  #     {:id=>233, :name=>"S1"},
  #     {:id=>1136, :name=>"V1"}
  #    ]=>[{:publisher=>{:id=>96, :name=>"P1"},
  #         :subject=>{:id=>233, :name=>"S1"},
  #         :volume=>{:id=>1136, :name=>"V1"},
  #         :chapter=>[{:id=>16155, :name=>"C1"}]
  #        },
  #        {:publisher=>{:id=>96, :name=>"P1"},
  #         :subject=>{:id=>233, :name=>"S1"},
  #         :volume=>{:id=>1136, :name=>"V1"},
  #         :chapter=>[{:id=>16158, :name=>"C2"}]
  #        }
  #       ]
  #   } 

第一个键值对传递给map块,块变量被分配。

k,v = h.first
  #=> [[{:id=>96, :name=>"P1"}, {:id=>233, :name=>"S1"}, {:id=>1136, :name=>"V1"}],
  #   [{:publisher=>{:id=>96, :name=>"P1"}, :subject=>{:id=>233, :name=>"S1"},
  #     :volume=>{:id=>1136, :name=>"V1"}, :chapter=>[{:id=>16155, :name=>"C1"}]},
  #    {:publisher=>{:id=>96, :name=>"P1"}, :subject=>{:id=>233, :name=>"S1"},
  #     :volume=>{:id=>1136, :name=>"V1"}, :chapter=>[{:id=>16158, :name=>"C2"}]}]]
k #=> [{:id=>96, :name=>"P1"}, {:id=>233, :name=>"S1"}, {:id=>1136, :name=>"V1"}]
v #=> [{:publisher=>{:id=>96, :name=>"P1"},
  #     :subject=>{:id=>233, :name=>"S1"},
  #     :volume=>{:id=>1136, :name=>"V1"},
  #     :chapter=>[{:id=>16155, :name=>"C1"}]},
  #    {:publisher=>{:id=>96, :name=>"P1"},
  #     :subject=>{:id=>233, :name=>"S1"},
  #     :volume=>{:id=>1136, :name=>"V1"},
  #     :chapter=>[{:id=>16158, :name=>"C2"}]}] 

并执行块计算。

h = { publisher: k[0], subject: k[1], volume: k[2] }
  #=> {:publisher=>{:id=>96, :name=>"P1"},
  #    :subject=>{:id=>233, :name=>"S1"},
  #    :volume=>{:id=>1136, :name=>"V1"}
  #   } 
a = v.each_with_object([]) { |f,a| a << f[:chapter] }
  #=> [[{:id=>16155, :name=>"C1"}], [{:id=>16158, :name=>"C2"}]] 
b = a.flatten
  #=> [{:id=>16155, :name=>"C1"}, {:id=>16158, :name=>"C2"}]
h.update(chapters: b)
  #=> {:publisher=>{:id=>96, :name=>"P1"},
  #    :subject=>{:id=>233, :name=>"S1"},
  #    :volume=>{:id=>1136, :name=>"V1"},
  #    :chapters=>[{:id=>16155, :name=>"C1"}, {:id=>16158, :name=>"C2"}]
  #   } 
可以使用

Hash#merge代替Hash#update

答案 1 :(得分:1)

怎么样:

    data = {}

    question_set.questions.each do |q|
      key = "#{q.publisher.id}:#{q.book_volume.subject.id}:#{q.book_volume_id}"
      if data[key].present?
        data[key][:chapter] << {id: q.section_id, name: q.section.name}
      else
        data[key] = {publisher: {id: q.publisher.id, name: q.publisher.name}, subject: {id: q.book_volume.subject.id, name: q.book_volume.subject.name}, volume: {id: q.book_volume_id, name: q.book_volume.name}, chapter: [{id: q.section_id, name: q.section.name}]}  
      end
    end

    result = data.values

使用发布商&id; id,subject&id; id和volume&#39; id的组合作为组合数据的唯一键。