Rails将多个参数合并在一起

时间:2015-05-07 13:47:57

标签: ruby-on-rails ruby ruby-on-rails-4

如何从我的权限哈希中将两个params合并在一起,共享相同的“school_id”和“plan_type”。然后删除从哈希合并的权限,只留下一个。还可以有两个以上匹配

[{"school_id"=>"1",
  "plan_type"=>"All",
  "view"=>"true",
  "create"=>"true",
  "approve"=>"true",
  "grant"=>"true",
  "region_id"=>nil},
 {"school_id"=>"1", "plan_type"=>"All", "edit"=>"true", "region_id"=>nil},
 {"school_id"=>"2",
  "plan_type"=>"All",
  "edit"=>"true",
  "grant"=>"true",
  "region_id"=>nil}]

def create_permissions(user, params)
  permissions = params[:permissions].values.map { |perm|
    if perm[:plan_type] == "" || perm[:plan_type] == "All Plans"
      perm[:plan_type] = "All"
    end
    @perm_type = get_permission_type(perm)
    case
    when 'school' then perm.merge(region_id: nil)
    when 'region' then perm.merge(school_id: nil)
    end
  }.tap { |permissions|
    new_permissions = []
    permissions.each do |perm|
      set_permissions = permissions.find {|x| (x != perm && x[:school_id] == perm[:school_id] && x[:plan_type] == perm[:plan_type]) }
    end
    params[:user][:region_ids] = permissions.map { |perm| perm[:region_id] }.compact
    params[:user][:school_ids] = permissions.map { |perm| perm[:school_id] }.compact
  }
end

输出:

[{"school_id"=>"1",
  "plan_type"=>"All",
  "view"=>"true",
  "create"=>"true",
  "approve"=>"true",
  "grant"=>"true",
  "region_id"=>nil},
  "edit"=>"true"
 {"school_id"=>"2",
  "plan_type"=>"All",
  "edit"=>"true",
  "grant"=>"true",
  "region_id"=>nil}]

2 个答案:

答案 0 :(得分:1)

school_id分组,然后通过合并哈希值进行缩小:

input.group_by { |e| e['school_id'] }
     .values
     .map { |v| p v.reduce(&:merge) }

要按许多字段分组,可以使用所需字段的数组,连接字符串,等等:

input.group_by { |e| [e['school_id'], e['plan_type']] }
     .values
     .map { |v| p v.reduce(&:merge) }

或者,保持漂亮的字幕:

input.group_by { |e| "School: #{e['school_id']}, Plan: #{e['plan_type']}" }
     .map { |k,v| [k, v.reduce(&:merge)] }
     .to_h

#⇒ {
#  "School: 1, Plan: All" => {
#      "approve" => "true",
#       "create" => "true",
#         "edit" => "true",
#        "grant" => "true",
#    "plan_type" => "All",
#    "region_id" => nil,
#    "school_id" => "1",
#         "view" => "true"
#  },
#  "School: 2, Plan: All" => {
#         "edit" => "true",
#        "grant" => "true",
#    "plan_type" => "All",
#    "region_id" => nil,
#    "school_id" => "2"
#  }
#}

答案 1 :(得分:0)

arr1 = arr.group_by { |e| [e["school_id"],e["plan_type"]] }.values
=> {["1", "All"]=>[{"school_id"=>"1", "plan_type"=>"All", "view"=>"true", "create"=>"true", "approve"=>"true", "grant"=>"true", "region_id"=>nil}, {"school_id"=>"1", "plan_type"=>"All", "edit"=>"true", "region_id"=>nil}], ["2", "All"]=>[{"school_id"=>"2", "plan_type"=>"All", "edit"=>"true", "grant"=>"true", "region_id"=>nil}]}

arr1.map{ |i| i.inject({}) { |sum, e| sum.merge e}}
=> [{"school_id"=>"1", "plan_type"=>"All", "view"=>"true", "create"=>"true", "approve"=>"true", "grant"=>"true", "region_id"=>nil, "edit"=>"true"}, {"school_id"=>"2", "plan_type"=>"All", "edit"=>"true", "grant"=>"true", "region_id"=>nil}]