Skip to content

Instantly share code, notes, and snippets.

View amitpatelx's full-sized avatar

Amit Patel amitpatelx

View GitHub Profile
@amitpatelx
amitpatelx / README.md
Last active September 17, 2015 03:40
blank? vs empty? - ActiveRecord

blank? will load the entire array, then check to see if the array is empty.

On the other hand, empty? asks the database for a count, and checks to see if that count is zero or not. This might not make a difference in small datasets (like development), but it can make a big difference in databases with large datasets (like production).

It will also make a huge difference in memory consumption when thousands of records are loaded vs a single integer.

Reference:

http://hashrocket.com/blog/posts/rails-quick-tips-easy-activerecord-optimizations

@amitpatelx
amitpatelx / README.md
Last active January 9, 2020 20:59
map vs pluck - Getting value of specific column using ActiveRecord

map will load the entire array, then iterate to collect the screen_names.

pluck asks the database for exactly what it needs and returns an array of just those items.

So in case of pluck, performance is gained and less memory is used for large datasets (like production).

Never use map on active record relations, use pluck instead.

If you're using pluck to pass values to a where use select instead

@amitpatelx
amitpatelx / command.sh
Created September 23, 2015 10:47
fatal: Could not read from remote repository error while deploying using Capistrano
#Run following commands in sequence to fix the issue
$ eval "$(ssh-agent -s)"
$ ssh-add ~/.ssh/id_rsa
$ cap production deploy
@amitpatelx
amitpatelx / test.rb
Created September 30, 2015 12:39 — forked from ParthivPatel-BTC/test.rb
Delete all database records
ActiveRecord::Base.connection.tables.map do |model|
unless ['schema_migrations', 'cic_user_profiles'].include?(model)
klass = model.capitalize.singularize.camelize.constantize
puts "Deleting #{klass}"
klass.delete_all
end
end
@amitpatelx
amitpatelx / documents_controller.rb
Created October 6, 2015 13:16
Dropzone with modal window
def create
@document = current_user.client.documents.new(document_params.merge!({ user_id: current_user.id }))
if @document.save
# do some stuff
else
@errors = @document.errors.full_messages.to_sentence
end
respond_to do |format|
@amitpatelx
amitpatelx / mine-open.desktop
Last active October 14, 2015 06:30
#Better Errors #Rubymine #Ubuntu
[Desktop Entry]
Encoding=UTF-8
Version=1.0
Type=Application
Terminal=false
Exec=/home/amit/scripts/mine-open.sh %u
Name[en_US]=MineOpen
Comment[en_US]=Small, easy-to-use program to access Rubymine
Name=MineOpen
Comment=Small, easy-to-use program to access Rubymine
@amitpatelx
amitpatelx / Gemfile
Last active October 26, 2015 11:56
BoTree's Typical Gemfile
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 4.2.4'
# Use pg as the database for Active Record
gem 'pg'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
@amitpatelx
amitpatelx / clear.js
Last active November 20, 2015 10:27
Clear selected file - HTML
// Once you select any file from file input, there is no way user can unselect it.
//Soluction
// Clear any input, including <input type='file' />
// http://stackoverflow.com/a/13351234/517483
window.smartReset = function (e) {
e.wrap('<form>').closest('form').get(0).reset();
e.unwrap();
}
@amitpatelx
amitpatelx / application.rb
Created March 23, 2016 11:17
How to share session between two Rails 4 applications?
# Use a different cache store in production.
# Configure this in both applications
elasticache = Dalli::ElastiCache.new('amit.patel.cfg.usw2.cache.amazonaws.com:11211')
config.cache_store = :dalli_store, elasticache.servers
@amitpatelx
amitpatelx / session_store.rb
Created March 23, 2016 11:29
Set the same session cookie name and domain scope in session_store.rb
# key and domain config must be same for both applications
Rails.application.config.session_store ActionDispatch::Session::CacheStore, :expire_after => 30.minutes, key: '_shared_session_key', domain: ".example.com"