Last active
April 24, 2020 08:08
-
-
Save denisoster/f61dcbdb20acc6cd8af1afa1c3714ac9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>> attributes_keys | |
{:country_ids=>:country_id, :group_type_ids=>:group_type_id, :region_ids=>:region_id, :city_ids=>:city_id} | |
(byebug) >> keys | |
[:country_ids, :city_ids] | |
(byebug) >> attributes_keys.slice(*keys) | |
{:country_ids=>:country_id, :city_ids=>:city_id} | |
(byebug) >> attributes_keys.slice(*keys).values | |
[:country_id, :city_id] | |
(byebug) >> attributes_keys.slice(*keys).values.zip(p) | |
[[:country_id, 1001], [:city_id, 1213]] | |
(byebug) >> Hash[attributes_keys.slice(*keys).values.zip(p)] | |
{:country_id=>1001, :city_id=>1213} | |
(byebug) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
attributes = { | |
:country_ids => [1001], | |
:group_type_ids => [], | |
:city_ids => [1213, 1214], | |
:region_ids => [] | |
}.delete_if { |_k, v| v.blank? } | |
attributes_keys = { | |
country_ids: :country_id, | |
group_type_ids: :group_type_id, | |
region_ids: :region_id, | |
city_ids: :city_id | |
} | |
keys = attributes.keys | |
attrs = keys.map { |key| attributes[key] } | |
product = attrs[0].product(*attrs[1..-1]) | |
product.map do |p| | |
Hash[attributes_keys.slice(*keys).values.zip(p)] | |
end | |
# => [{:country_id=>1001, :city_id=>1213}, {:country_id=>1001, :city_id=>1214}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.