0

I have the following Nested Hash

def testy_user
 {
  search_templates: { 
     profile_1: { default_search_form: 'simple', name: 'Automation Profile', default_profile: 'yes', profile_set: 'standard', ref_required: 'no', fk_search_type_id: '1' },
     profile_2: { default_search_form: '', name: 'Potential Links', default_profile: 'no', profile_set: 'standard', ref_required: 'no', fk_search_type_id: '1' },
     profile_3: { default_search_form: '', name: 'Insolvency', default_profile: 'no', profile_set: 'standard', ref_required: 'no', fk_search_type_id: '1' },
     profile_4: { default_search_form: '', name: 'Mortality', default_profile: 'no', profile_set: 'standard', ref_required: 'no', fk_search_type_id: '1' },
     profile_5: { default_search_form: '', name: 'Mortality', default_profile: 'no', profile_set: 'standard', ref_required: 'no', fk_search_type_id: '1' },
     profile_6: { default_search_form: '', name: 'PRS', default_profile: 'no', profile_set: 'standard', ref_required: 'no', fk_search_type_id: '1' },
     profile_7: { default_search_form: '', name: 'Neighbour', default_profile: 'no', profile_set: 'standard', ref_required: 'no', fk_search_type_id: '1' },
     profile_8: { default_search_form: '', name: 'Smartlinks', default_profile: 'no', profile_set: 'standard', ref_required: 'no', fk_search_type_id: '1' },
     profile_9: { default_search_form: '', name: 'Property', default_profile: 'no', profile_set: 'standard', ref_required: 'no', fk_search_type_id: '1' },
     profile_10: { default_search_form: '', name: 'Occupants', default_profile: 'no', profile_set: 'standard', ref_required: 'no', fk_search_type_id: '1' }
     }
 }
end

I am putting together some logic that will iterate through each "profile" and write to my database (using Mysql gem)

testy_user[:search_templates].select { |p| p=[/^profile_/] }.each do |template|
  statement = @db.prepare('INSERT INTO profile (fk_user_id, default_search_form, name, default_profile, ref_required, fk_search_type_id) VALUES(?,?,?,?,?,?)')
  statement.execute(@user_id, template[:default_search_form], template[:name], template[:default_profile], template[:ref_required], template[:fk_search_type_id])
end

As i am now in this loop each template is an array and i can no longer access the keys as symbols (I get TypeError: no implicit conversion of Symbol into Integer)

Do i need to convert each item of the array back into a hash or is there a cleaner way of approaching this ?

Thanks

0

1 Answer 1

2

Your assumption of the problem you are facing is irrelevant to the actual problem.

You select condition is wrong (it is not even a condition, it is an assignment). Moreover, you should explicitly define where is key (i.e. key) and where is corresponding to it element (i.e. _ – underscore means, that you are not using it in the context of the block):

testy_user[:search_templates].select{|key, _| key =~ /^profile_/ }.each do |_, template|
  <your code>
end
Sign up to request clarification or add additional context in comments.

2 Comments

apologies, typo meant i missed the curly braces with my testy_user method
Thank you for your help and clarifying my mistake

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.