It's a bit unclear to me what you want to achieve here. Looking at your example, the easiest solution would be to change the keys of entry_hash
using .downcase
and adding _id
. For the value, you could check if it's an array and if so, use the first value.
output = {}
entry_hash.each do |key, value|
output[key.downcase + '_id'] = value.kind_of?(Array) ? value[0] : value
end
This assumes of course that the keys in the hash are the nouns for the column names in the array. The code above will not work if the names are more complex (e.g. CamelCaseName
or snake_case_id
). Rails comes with ActiveSupport that can help you there, but this is a totally different question: Converting camel case to underscore case in ruby
If array and hash don't share the same names there is no easy way to do this automatically. Hash doesn't guarantee the order of its elements, so iterating through both and mapping values like in the above snippet won't work reliably.