Ruby on Rails: Iterate Through Nested Params - ruby-on-rails

I want to iterate through submitted params. Params array could be up to three level. Currently I can iterate only first level like this.
params[:job].each do |v, k|
#do something with k value, only if it's string. If it's array then find its string value.
end
How do I iterate params when you don't know what you are expecting?

One easy way is to use is_a?
if v.is_a? Array
# do whatever
elsif v.is_a? String
# do whatever
else
# something else
end
Some kind of recursive solution might be best here, such as:
def handle_hash hash
hash.each do |k, v|
if v.is_a? Hash
handle_hash v
elsif v.is_a? String
# handle string
end
end
end
And then you can just call handle_hash params[:job]

def nested_params(nested_hash={})
nested_hash.each_pair do |k,v|
case v
when String, Fixnum then
dosomething(v)
when Hash then nested_params(v)
else raise ArgumentError, "Unhandled type #{v.class}"
end
end
end

Related

Collect KeyErrors from Ruby hash into array

I need to extract multiple fields from hash. But I respect my client and I want to gather all missed fields instead of returning it one by one. My idea was to use #fetch, intercept error with KeyError, put error.key into instance variable array and return proper error explanation with full list of missed keys.
Something like that
class Extractor
def initialize hash
#hash = hash
#missed_keys = []
end
def call
extract_values
return "Missed keys: #{#missed_keys.join(', ')}" if #missed_keys.present?
rescue KeyError => e
puts 'Field was missed'
#missed_keys << e.key
return 'Error'
end
private
def extract_values
{
value_1: #hash.fetch(:required_field_1),
value_2: #hash.fetch(:required_field_2),
value_3: #hash.fetch(:required_field_3)
}
end
end
When I try to process hash without required fields I got 'Error' after the first missed field:
pry(main)> Extractor.new(hash: {}).call
Field was missed
=> "Error"
Any clues?
DrySchema and other hash validators are not an option.
An issue with the provided solution is that the extracted values are never returned in the happy path (which presumably is important?). The call method is also stateful / non-idempotent. Subsequent calls to call will duplicate the missing-keys.
Finally - not sure how it's being used, but I don't love a method that returns either a hash or a string.
An alternative that attempts to follow a more functional pattern might look like:
class Extractor
attr_reader :hash, :missed_keys, :required_keys
def initialize hash
#hash = hash
#missed_keys = []
#required_keys = [:required_field_1, :required_field_2, :required_field_3]
end
def call
validate_keys_exist!
extract_values
end
private
def validate_keys_exist!
missed_keys = find_missing_keys
raise MissingKeysError, "Missed keys: #{missed_keys.join(', ')}" if missed_keys.any?
end
def find_missing_keys
required_keys - hash.keys
end
def extract_values
hash.slice(*required_keys)
# not sure if you need to map the keys to new values.
# if so you can iterate over a hash of `from: :to` pairs instead of the
# required_keys array.
end
end
Ok, I got it. The reason is in intercept level and method closures.
In aforementioned implementation Ruby tried to execute call method, got an error and exits.
If we rework it like that:
class Extractor
def initialize hash
#hash = hash
#missed_keys = []
end
def call
extract_values
return "Missed keys: #{#missed_keys.join(', ')}" if #missed_keys.present?
end
private
def extract_values
{
value_1: #hash.fetch(:required_field_1),
value_2: #hash.fetch(:required_field_2),
value_3: #hash.fetch(:required_field_3)
}
rescue KeyError => e
puts 'Field was missed'
#missed_keys << e.key
nil
end
end
it looks better, but still not what we wanted:
pry(main)> Extractor.new(hash: {}).call
Field was missed
=> "Missed keys: required_field_1"
This is because ruby tried to execute extract_values method, encounters first missed value and exits
So the solution as follow:
class Extractor
def initialize hash
#hash = hash
#missed_keys = []
end
def call
extract_values
return "Missed keys: #{#missed_keys.join(', ')}" if #missed_keys.present?
end
private
def extract_values
{
value_1: fetch_value(:required_field_1),
value_2: fetch_value(:required_field_2),
value_3: fetch_value(:required_field_3)
}
end
def fetch_value(key)
#hash.fetch(key)
rescue KeyError => e
puts 'Field was missed'
#missed_keys << e.key
nil
end
end
Extractor.new(hash: {}).call
Field was missed
Field was missed
Field was missed
=> "Missed keys: required_field_1, required_field_2, required_field_3"
Error interception is accomplished on the fetch_value level and Ruby skips required values one by one

How can I create an Active Admin table in a custom page and insert JSON data?

I'm trying to build a table of JSON data in a custom page in Active Admin. The JSON response is fairly deeply nested so I'm using a lot of loops. Here's as far as I've been able to get/some of what I've tried:
panel "Boxes" do
boxes.each do |box| #isolate each hash item in the array
# table_for box.values do
box.each do |key, value|
if value.is_a?(Hash) #if value of a hash is a hash
columns do
value.each do |k, v| #iterate through the hash
text_node "#{k}: #{v}"
end
end
elsif value.is_a?(Array) #there's only one value that's an array & the key is "Products"
columns do
value.each do |products_array|
columns do
products_array.each do |k, v|
if v.is_a?(Hash)
v.each do |kk, vv|
if vv.is_a?(Hash)
vv.each do |kkk, vvv|
text_node "#{kkk}: #{vvv}, "
end
else
text_node "#{kk}: #{vv}, "
end
end
else
text_node "#{k}: #{v}, "
end
end
end
end
end
else
# key.each do
# column key
# end
end
end
# end
end
I'm looking for general guidelines as to how to make a table in a custom Active Admin page as well as how to access/display deeply nested array/hash attributes. Thanks!
Try a recursive helper to convert the JSON to the string you want:
def nested_hash_to_s(h)
h.map { |k, v| v.is_a?(Hash) nested_hash_to_s(v) : "#{k}: #{v}" }.join(", ")
end
then your column simplifies to:
columns do
text_node nested_hash_to_s(products_array)
end
There might be other issues, but that's a start.

Accessing elements of a ruby hash

Ruby 2.15
I defined the following hash:
test = Hash.new
test["foo"] = {
'id' => 5,
'lobbyist_id' => 19,
'organization_id' => 8
}
If I do
test.each do |t|
print t["id"]
end
I get:
TypeError: no implicit conversion of String into Integer
from (irb):1571:in `[]'
How do I access the elements, using an each loop?
Answer:
test.each do |t|
t.each do |t1|
puts t1["id"]
end
end
With a Hash, iteration is made through key first, then value. So have your block use what you need.
test.each do |key|
puts key
end
test.each do |key, value|
puts key
puts value
end
There are also
test.each_key do |key|
puts key
end
test.each_value do |value|
puts value
end
Sidenote: id is inside test["foo"], so maybe you'd need 2 loops
To get id from your hash directly:
test["foo"]["id"]
test["foo"].each {|k, v| puts "#{k}: #{v}" }
In your example we assume you've previously done:
test = Hash.new
In your example variable test is a hash and foo is a key who's value contains a hash of key values. If you want to target those, you'll need to loop over them
test['foo'].each do |k,v|
puts "my key is #{k}"
puts "it's value is {v}
end
If you want to do both at the same time:
test.each do |k,v|
puts "base hash key #{k}"
puts "base hash value #{v}"
v.each do |kk,vv|
puts "key #{kk}"
puts "value #{vv}"
end
end

How do I iterate through a table and create a hash for each value?

I have two tables. One for accounts and another for keywords. I would like to iterate over all of the keywords and store each one in a hash--grouped by the account ID that added the keyword. The code that I have below doesn't add each keyword to the hash. For example, I have an account that has 2 keyword entries. My code skips the first entry and only adds the second entry to the hash.
#keyword_hash = {}
#account.each do |key, value|
#keywords.where(:profile_id => key).each do |keyword|
#keyword_hash[key] = keyword.entry
end
end
puts #keyword_hash
How can I modify the above code so that I add each keyword entry for a particular account to the hash?
I would like to be able to do #keyword_hash[6] and get keyword1, keyword2, keyword3, etc. for that account. Thanks!
Make an array [keyword1, keyword2, keyword3, etc.] and then add it to hash
**
#keyword_hash = {}
#account.each do |key, value|
arr = []
#keywords.where(:profile_id => key).each do |keyword|
arr << keyword.entry
end
#keyword_hash[key] = arr
end
puts #keyword_hash
**
Try this code
#keyword_hash = Hash.new { |h, k| h[k] = [] }
#account.each do |key, value|
#keywords.where(:profile_id => key).each do |keyword|
#keyword_hash[key] << keyword.entry
end
end
puts #keyword_hash
The mistake you are doing is that you are storing a single value against each key in your #keyword_hash hash. so when your code writes second value against account key, it replaces the previous value instead of adding second one.
Edit: Thank you #mudasobwa for correction regarding shared default value.
#keyword_hash = Hash.new { |hash, key| hash[key] = [] }
#keywords.group_by{ |k| k.profile_id }.each do |key,value|
#keyword_hash[key] = value.map(&:entry)
end
puts #keyword_hash
After doing some more research, I found the above solution. I used #jvillian's suggestion about group_by, and I found this article that showed me how to initialize the hash.

Ruby/Rails Iterate through array and save to db?

I want to put each string from #enc into each field of column_name as a value
#enc=["hUt7ocoih//kFpgEizBowBAdxqqbGV1jkKVipVJwJnPGoPtTN16ZAJvW9tsi\n3inn\n", "wGNyaoEZ09jSg+/IclWFGAXzwz5lXLxJTUKqCFIiOy3ZXRgdwFUsNf/75R2V\nZm83\n", "MPq3KSzDzLvTeYh+h00HD+5FAgKoNksykJhzROVZWbIJ36WNoBgkSoicJ5wx\nog0g\n"]
Model.all.each do |row|
encrypted = #enc.map { |i| i}
row.column_name = encrypted
row.save!
end
My code puts all strings from array #enc into a single field?
I do not want that.
Help
Rails by default won't allow mass assignment. You have to whitelist parameters you want permitted. Have you tried doing something like the following?
#enc.each do |s|
cparams = create_params
cparams[:column_name] = s
Model.create(cparams)
end
def create_params
params.permit(:column_name)
end
You will need to specify the column names you are saving to. By setting each column separately you can also avoid mass-assignment errors:
#enc=["hUt7ocoih//kFpgEizBowBAdxqqbGV1jkKVipVJwJnPGoPtTN16ZAJvW9tsi\n3inn\n", "wGNyaoEZ09jSg+/IclWFGAXzwz5lXLxJTUKqCFIiOy3ZXRgdwFUsNf/75R2V\nZm83\n", "MPq3KSzDzLvTeYh+h00HD+5FAgKoNksykJhzROVZWbIJ36WNoBgkSoicJ5wx\nog0g\n"]
model = Widget.new
column_names = [:column1, :column2, :column3]
#enc.each_with_index do |s, i|
model[column_names[i]] = s
end
model.save
I think you are looking for something like this:
#enc.each do |str|
m = Model.new
m.column_name = str
m.save
end

Resources