Lumped tweets

Just marks

ActiveRecordでレコードをARインスタンス化しないでハッシュで取り扱う

背景

ams = AwesomeModel.where(id: [1,2,3])
#=> <ActiveRecord::Relations ... >

で、ams は AwesomeModel のインスタンスであるが、インスタンス化したくないときがある。

どうするか

AwesomeModel.connection.exec_query(AwesomeModel.where(id: [1,2,3]).to_sql).to_hash
#=> [{id: 1, attribute: 'hoge'}, {id: 2, attribute: 'huga'}]

頑張ろう。