Skip to content

Instantly share code, notes, and snippets.

@arvind02
arvind02 / rails-jsonb-queries
Created August 25, 2022 19:47 — forked from mankind/rails-jsonb-queries
Ruby on Rails-5 postgresql-9.6 jsonb queries
http://stackoverflow.com/questions/22667401/postgres-json-data-type-rails-query
http://stackoverflow.com/questions/40702813/query-on-postgres-json-array-field-in-rails
#payload: [{"kind"=>"person"}]
Segment.where("payload @> ?", [{kind: "person"}].to_json)
#data: {"interest"=>["music", "movies", "programming"]}
Segment.where("data @> ?", {"interest": ["music", "movies", "programming"]}.to_json)
Segment.where("data #>> '{interest, 1}' = 'movies' ")
Segment.where("jsonb_array_length(data->'interest') > 1")
@arvind02
arvind02 / mac-setup-redis.md
Created September 7, 2021 12:56 — forked from tomysmile/mac-setup-redis.md
Brew install Redis on Mac

type below:

brew update
brew install redis

To have launchd start redis now and restart at login:

brew services start redis
@arvind02
arvind02 / active_record.rb
Created August 25, 2021 16:30 — forked from jackrg/active_record.rb
Add bulk import functionality to Rails Active Record (add this file to config/initializers, call <model>.import!(array-of-record-hashes))
class ActiveRecord::Base
def self.import!(record_list)
raise ArgumentError "record_list not an Array of Hashes" unless record_list.is_a?(Array) && record_list.all? {|rec| rec.is_a? Hash }
return record_list if record_list.empty?
(1..record_list.count).step(1000).each do |start|
key_list, value_list = convert_record_list(record_list[start-1..start+999])
sql = "INSERT INTO #{self.table_name} (#{key_list.join(", ")}) VALUES #{value_list.map {|rec| "(#{rec.join(", ")})" }.join(" ,")}"
self.connection.insert_sql(sql)
@arvind02
arvind02 / example1
Created July 24, 2020 05:56 — forked from daipresents/example1
Ruby rest-client file upload as multipart
require 'rest-client'
RestClient.get(url, headers={})
RestClient.post(url, payload, headers={})
@arvind02
arvind02 / s3_folder_upload.rb
Created December 12, 2019 06:14 — forked from fleveque/s3_folder_upload.rb
Upload folder to S3 recursively with ruby, multi threads and aws-sdk v2 gem, based on http://avi.io/blog/2013/12/03/upload-folder-to-s3-recursively/
#!/usr/bin/env ruby
require 'rubygems'
require 'aws-sdk'
class S3FolderUpload
attr_reader :folder_path, :total_files, :s3_bucket, :include_folder
attr_accessor :files
# Initialize the upload class
@arvind02
arvind02 / readme.md
Created November 5, 2019 05:44 — forked from maxivak/readme.md
Integrating Gem/Engine and Main Rails App
@arvind02
arvind02 / heap.rb
Created July 11, 2019 12:41 — forked from bogdanovich/heap.rb
Heap data structure in ruby
class Heap
def initialize(type = :min)
@type = type
if @type == :min
@heap = [-1.0/0]
@compare = ->(a,b) { (a <=> b) < 0 }
elsif @type == :max
@heap = [1.0/0]
@compare = ->(a,b) { (b <=> a) > 0 }
else