Merubah Nama Attribute yang Panjang pada Collection of Object di Rails
Created at: 2023-09-17
Author by: BanditHijo
Pendahuluan
Pada catatan kali ini saya ingin memodifikasi output dari nama attribute yang terlalu panjang menjadi lebih pendek di Ruby on Rails.
Masalah
Saya memiliki method geofences yang mengembalikan colllection of hash data dari model geofeonce.
Model geofence memiliki attributes,
:id, :created_at, :updated_at, :name, :description, :coordinates, :incoming_notification, :outgoing_notification
Saya hanya ingin mengambil attributes: :id, :incoming_notification, :outgoing_notification.
app/geofences_controller.rb
1def geofences2 {3 geofences: Geofence.all.order(id: :asc).as_json(only: [:id, :incoming_notification, :outgoing_notification])4 }5endOutputnya,
1{2 "geofences": [3 {4 "id": 1,5 "incoming_notification": true,6 "outgoing_notification": false7 },8 {9 "id": 2,10 "incoming_notification": false,11 "outgoing_notification": true12 },13 {14 "id": 3,15 "incoming_notification": true,16 "outgoing_notification": false17 },18 {19 "id": 4,20 "incoming_notification": false,21 "outgoing_notification": true22 }23 ]24}25Karena nama attribute :incoming_notification dan :outgoing_notification terlalu panjang, saya akan ganti menjadi :in dan :out.
Pemecahan Masalah
Algoritma Pertama
app/geofences_controller.rb
1def geofences2 {3 geofences: Geofence.all.order(id: :asc)4 .as_json(only: [:id, :incoming_notification, :outgoing_notification])5 .each { |attribute|6 attribute.tap { |key|7 key[:in] = key.delete('incoming_notification')8 key[:out] = key.delete('outgoing_notification')9 }10 }11 }12endAlgoritma Kedua
app/geofences_controller.rb
1def geofences2 geofences = Geofence.all.order(id: :asc)34 transformed_geofences = geofences.map do |geofence|5 {6 id: geofence.id,7 in: geofence.incoming_notification,8 out: geofence.outgoing_notification9 }10 end1112 { geofences: transformed_geofences }13endSaya menulis 2 pendekatan. Namun, saya cenderung memilih algoritma kedua.
Alasannya adalah,
Database Query Optimization:
- algoritma pertama, diambil semua data, lalu dilakukan filtering dan tranformation dengan
.as_json - algoritman kedua, hanya mengambil attribute yang diperlukan
:id,:incoming_notification,:outgoing_notificationdengan.map(), hal ini berpotensi untuk meminimalisir query time ke database
Reduced Object Mutation:
- algoritman pertama, menggunakan
.eachdengan.tapuntuk memodifikasi Hash Object, hal ini terkadang dapat menyebabkan behaviour yang tidak dapat diprediksi sehingga menimbulkan bug. - algoritma kedua, langsung membuat Hash Object dengan attribute yang diperlukan tanpa meodifikasi data aslinya, tentu saja ini menjadi pendekatan yang cukup aman
Hasilnya,
1{2 "geofences": [3 {4 "id": 1,5 "in": true,6 "out": false7 },8 {9 "id": 2,10 "in": false,11 "out": true12 },13 {14 "id": 3,15 "in": true,16 "out": false17 },18 {19 "id": 4,20 "in": false,21 "out": true22 }23 ]24}25Pesan Penulis
Terima kasih sudah mampir yaa.
Referensi
***
Rizqi Nur Assyaufi (bandithijo)
My journey kicks off from reading textbooks as a former Medical Student to digging bugs as a Software Engineer – a delightful rollercoaster of career twists. Embracing failure with the grace of a Cat avoiding water, I've seamlessly transitioned from Stethoscope to Keyboard. Armed with ability for learning and adapting faster than a Heart Beat, I'm on a mission to turn Code into a Product.