I need to hashify ActiveRelation! For example I have such AR
arel = MyClass.page(options[:page]).per(options[:per])
Is here a way simple to create hash with primary keys of MyTable as keys in hash
Ex1:
[#<Object id: 44, name: "name44" >, #<Object id: 2, name: "name2" >, #<Object id: 110, name: "name110">]
=>
{44=>"name44", 2=>"name2",110=>"name110"}
Ex2:
[#<Object id: 44, name: "name44" >, #<Object id: 2, name: "name2" >, #<Object id: 110, name: "name110">]
=>
{44=>{:id=>44, :name=>"name44", 2=>{:id=>2,:name=>"name2"},110=>{:id=>110,:name=>"name110"}}
Try index_by
:
MyClass.limit(20).all.index_by(&:id)
http://api.rubyonrails.org/classes/Enumerable.html#method-i-index_by
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With