Thursday, September 7, 2017

To Fix bundler issue "Bundler::Fetcher::CertificateFailureError"

on MAC system

While updating any gem or adding new gem, we need to use bundle install

But most of the time we may face issue


Fetching source index from https://rubygems.org/

In Console, we may get like this

Retrying fetcher due to error (2/4): Bundler::Fetcher::CertificateFailureError Could not verify the SSL certificate for https://rubygems.org/.
There is a chance you are experiencing a man-in-the-middle attack, but most likely your system doesn't have the CA certificates needed for verification. For information about OpenSSL certificates, see http://bit.ly/ruby-ssl. To connect without using SSL, edit your Gemfile sources and change 'https' to 'http'.
Retrying fetcher due to error (3/4): Bundler::Fetcher::CertificateFailureError Could not verify the SSL certificate for https://rubygems.org/.
There is a chance you are experiencing a man-in-the-middle attack, but most likely your system doesn't have the CA certificates needed for verification. For information about OpenSSL certificates, see http://bit.ly/ruby-ssl. To connect without using SSL, edit your Gemfile sources and change 'https' to 'http'.
Retrying fetcher due to error (4/4): Bundler::Fetcher::CertificateFailureError Could not verify the SSL certificate for https://rubygems.org/.
There is a chance you are experiencing a man-in-the-middle attack, but most likely your system doesn't have the CA certificates needed for verification. For information about OpenSSL certificates, see http://bit.ly/ruby-ssl. To connect without using SSL, edit your Gemfile sources and change 'https' to 'http'.
Could not verify the SSL certificate for https://rubygems.org/.
There is a chance you are experiencing a man-in-the-middle attack, but most likely your system doesn't have the CA certificates needed for verification. For information about OpenSSL certificates, see
http://bit.ly/ruby-ssl. To connect without using SSL, edit your Gemfile sources and change 'https' to 'http'.



To Fix this,

$ brew tap raggi/ale $ brew install openssl-osx-ca





Thanks


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!!!

Sunday, February 1, 2015

Setter and Getter method without using attr_accessor with DRY codes


Ruby code with custom Getter and Setter methods without using "attr_accessor" method


class Klass

  def methods(*mtds)

    mtds.each do |mtd|

      self.class.class_eval("def #{mtd}; @#{mtd}; end")

      self.class.class_eval("def #{mtd}=(val); @#{mtd}=val;end")
   end

  end
end


eg.:

obj = Klass.new

obj.methods 'mtd1','mtd2'


obj.mtd1 = "test1"
obj.mtd2 = 'test2'

o/p:
puts obj.mtd1 = test1
puts obj.mtd2 = test2