0

I am trying to form an array from the result I got from querying the database

industries = Industry.find_by_name(j['Categories']).industries

The result that i get is as following

[#<Industry id: 3717, staffroom_type: "Industry", name: "Home Service", created_at: "2016-01-19 02:33:30", updated_at: "2016-01-19 05:25:53", parent_id: nil, user_cannot_join_shortlists: false, location: "SYDNEY, NSW, 2000", latitude: -33.8674769, longitude: 151.2069776, shortlist_introduction_email_subject: nil, shortlist_introduction_email_body: nil, external_job_url_enabled: false, deleted: false, deleted_at: nil, account_id: 3506, create_group_permission: false, create_role_permission: false, edit_group_permission: true, uuid: "70ef9351-f517-40a9-a300-8c1078da033a", staffroom_image_file_name: nil, staffroom_image_content_type: nil, staffroom_image_file_size: nil, staffroom_image_updated_at: nil, staffroom_image_repository: "production", cached_staffroom_image_id: nil, industry_type_id: nil, company_id: nil, billed_to: "employer", admin_user_id: nil, send_job_notifications: true, do_not_feature_jobs: false, company_type: nil, company_type_other: nil, company_restriction: nil, shortlist_count: nil>,
 #<Industry id: 1624, staffroom_type: "Industry", name: "Aged and Disability Care", created_at: "2015-04-13 02:07:53", updated_at: "2017-05-30 10:49:17", parent_id: nil, user_cannot_join_shortlists: false, location: "NOT PROVIDED (Assuming Sydney, 2000)", latitude: nil, longitude: nil, shortlist_introduction_email_subject: nil, shortlist_introduction_email_body: nil, external_job_url_enabled: false, deleted: false, deleted_at: nil, account_id: 3506, create_group_permission: false, create_role_permission: false, edit_group_permission: true, uuid: "f4b7bac3-3587-4056-a88f-9a9e98c42197", staffroom_image_file_name: nil, staffroom_image_content_type: nil, staffroom_image_file_size: nil, staffroom_image_updated_at: nil, staffroom_image_repository: "production", cached_staffroom_image_id: nil, industry_type_id: nil, company_id: nil, billed_to: "employer", admin_user_id: nil, send_job_notifications: true, do_not_feature_jobs: false, company_type: nil, company_type_other: nil, company_restriction: nil, shortlist_count: nil>]

I would like to convert this result into an array that just looks like this

['Home Service', 'Aged and Disability Care']

These are the name of industries that i get from database.

This is what i tried

      industries.each do |industry|
        ind[] = industry['name']

      end

but I get an error

NoMethodError: undefined method `each' for #

What am I doing wrong here?

This is what j['Categories'] looks like

Disability Support Worker: Disability Support Worker
1
  • Quick question - if you're using find_by_name or where(name: ...) from j['Categories'] and looking to select the industry's name... could you not just use the value of j['Categories']? (If not, @Jordan has the right answer) :) Commented May 30, 2017 at 16:40

1 Answer 1

4

Use the built-in pluck method:

Industry.where(name: j['Categories']).pluck(:name)

This will be much more performant than map: The map solution will retrieve every column and instantiate an Industry object for each record, then call the name method on each object. pluck will retrieve only the given column and return an array of strings.

Sign up to request clarification or add additional context in comments.

1 Comment

Dear downvoter: Please take a moment to leave a comment explaining the reason for your downvote. If my answer is lacking I'd appreciate the opportunity to improve it.

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.