Answer the question
In order to leave comments, you need to log in
Ruby on Rails how to convert ActiveRecord:Relation selection to hash?
Hello
Tell me how to convert such a selection to a hash where the key will be the external_key field, which is 100% unique.
Sample:
Customer.all
[#<Customer id: 1, name: "Рога", external_key: "00002256">, #<Customer id: 1, name: "Копыта", external_key: "00002257">]
{'00002256' => 1, '00002257' => 2}
Answer the question
In order to leave comments, you need to log in
hash = {}
Customer.all.find_each { |c| hash[c.external_key] = c.id }
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question