1

I'm making a lookup hash on a Model from a unique name to a unique ID

lookup_hash = Hash[*Country.pluck(:name, :id).flatten]
=>
lookup_hash = {"America" => 12, "Brazil" => 2, "China" => 51...}

That way I have an object that requires a single SQL call that I can use to repeatedly reference in getting the corresponding IDs from an array of names.

It seems useful enough to wonder if there is a shorthand for this.

Nelson C.
  • 11
  • 1

1 Answers1

0

You need not do splat (*) or flatten.

Hash[Country.pluck(:name, :id)]
nishu
  • 1,493
  • 11
  • 26