[RAILS] Ruby Hash stack overflows when there are many arguments

I was using ActiveRecord to create a Hash with key => value.

@users = Hash.send :[], *ActiveRecord::Base.connection.execute('SELECT `id`, `name` FROM `users`').map {|x| x}.flatten
@users[1]
# => "User 1"

Since Hash [* Array] is used, stack overflow will occur if the number of records fetched by execute exceeds a certain level.

There is a problem with both how to use ActiveRecord and how to make Hash.

@users = User. User.pluck(:id, :name).to_h
@users[1]
# => "User 1"

Get the required column with .pluck and hash it with .to_h

Recommended Posts

Ruby Hash stack overflows when there are many arguments
Overload when method arguments are inherited / implemented
When there are environment variables in Java tests
[Ruby on Rails] If there are duplicate records when registering data, do not register