Wednesday, February 8, 2017

Psql Update query to replace json data key(stored as json field)

Hello Guys,

   In one of my project,  I need to replace json key without changing the values in the psql. I could able to find replace text in psql side. We need to write separate function to update the json key.

For my purpose, i have googled around and find the idea or converting the field as text and convert back to json was easier and better(in my project).

If we are not converting to text type, psql will wont allow you to replace string in json field format.


Here is the snippet for it.

Please note, data field is json type
UPDATE table_name SET <field_name> = REPLACE(<field_name>::TEXT, 'old_string', 'new_string')::json;

eg:- UPDATE customer_details SET data = REPLACE(data::TEXT, '"map":', '"maps":')::json;


You can also replace for jsonb type as well by replacing above ::json with ::jsonb



Please note, this is my personal opinion and there might be other best ways to do the same stuff.


Hope!! this will help few folks


Cheers coding!!!

Rails Active Model sorting for text field in psql db

Hello Guys,
     In one scenario of my project, need to show all address with zipcode(stored as text field) sorting. It's good to make sort in db level as this will have better performance when comparing with ruby block.

in ../models/address.rb

def with_zipcode
   Address.where(customer_id: 1).order('cast(zipcode as double precision)')
end


This query will provide all customer address with zipcode in ascending order.


Thanks!!

Cheers up with Coding!!

Monday, February 6, 2017

Active record uniqueness validation with soft delete feature

Soft Delete:
   we will maintain the record with updating few flag to check whether its active or not deleted like active_flag.  We can update the same field again by updating the active_flag in future with some feature or some other mechanism

Hard Delete:
   We will remove the record without have a copy.


In one of my recent application, I have the requirement of validating uniqueness constraint with  soft delete feature. We have active_flag field to check whether the record is deleted or not. We have few gems to maintain this status. I have used  rails gem act_as_paranoid  to have in-build validation. In this case, we have deleted_at field to check the status of the record. This gem will update all the query to make sure this field has NULL value.

like,
Console:
    User.find(120)

It will run Query as follows:
   select `users.*` from users where `users.id` = 120 and `users.deleted_at` is NULL


In another case without using any gem, we need to write the custom validation to override this

in ../models/user.rb

validates :name, uniqueness: {scope: :active_flag, conditions: -> {where(active_flag: true)}, 'An Active user with same name already available' }


Note: There are many gems available for making soft delete feature. I have tried personally above act_as_paranoid. I am not sure about any other rails gem.

Hope this snippet helped to few folks.

Enjoy!!!



Accessing localhost Rails application with other machine or device with in same network

In the recent version of Rails, we could not able check with the system ip

ie:- 192.168.222.34:3000

we could able to get the application page.

To solve this,  start server by
     rails s -b 0.0.0.0


This will bind the rails server to listen with system ip



Hope it will help few folks!

Enjoy!!!