Skip to content

Instantly share code, notes, and snippets.

View amitpatelx's full-sized avatar

Amit Patel amitpatelx

View GitHub Profile
@amitpatelx
amitpatelx / Gemfile
Created July 18, 2014 09:53
Rails Developers' Gems for Debugging
group :development do
# rake routes is slow and painful. view routes in browser - http://localhost:3000/rails/routes.
gem 'sextant' #default in Rails 4
# turns off the Rails asset pipeline log. suppresses asset messages in your development log
gem 'quiet_assets'
# powerful alternative to the standard IRB
# features: syntax highlighting, a flexible plugin architecture, runtime invocation and source and documentation browsing.
gem 'pry-rails'

Originally published in June 2008

When hiring Ruby on Rails programmers, knowing the right questions to ask during an interview was a real challenge for me at first. In 30 minutes or less, it's difficult to get a solid read on a candidate's skill set without looking at code they've previously written. And in the corporate/enterprise world, I often don't have access to their previous work.

To ensure we hired competent ruby developers at my last job, I created a list of 15 ruby questions -- a ruby measuring stick if you will -- to select the cream of the crop that walked through our doors.

What to expect

Candidates will typically give you a range of responses based on their experience and personality. So it's up to you to decide the correctness of their answer.

@amitpatelx
amitpatelx / gist:2b4885144c63359c2e06
Last active August 29, 2015 14:23
Remove Duplicate Active Record Objects
pins = Pin.group(:name).having('count("name") > 1').count(:name)
{"Adam"=>2,
"Beulings"=>2,
"Bord'Eau (Hotel De L'Europe Amsterdam)"=>2,
"Ciel Bleu (Hotel Okura Amsterdam)"=>2,
"Coffee Plaza"=>2,
"Da Portare Via"=>3,
"Dappertutto"=>2,
"En Pluche"=>2,
@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 / 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 / diff_before_and_let_spec.rb
Created March 16, 2012 09:55
Show usage difference between before and let helper of RSpec
# Using before method
before do
@customer = Customer.new(:name => "Amazon")
end
it "should be valid" { @customer.should be_valid }
# Using let method
let(:customer) { Customer.new(:name => "Amazon") }
@amitpatelx
amitpatelx / let_spec.rb
Created March 16, 2012 09:15
use let to define memoized helper method
$count = 0
describe "let" do
let(:count) { $count += 1 }
it "memoizes the value" do
count.should eq(1)
count.should eq(1)
end
it "is not cached across examples" do
@amitpatelx
amitpatelx / stack_spec.rb
Created March 16, 2012 10:37
This snippet(from The RSpec Book) shows how to define example group in RSpec.
require "rspec/expectations"
class Thing
def widgets
@widgets ||= []
end
end
describe Thing do
before(:all) do
# Renders the template located in [TEMPLATE_ROOT]/weblog/show.r(html|xml) (in Rails, app/views/weblog/show.erb)
render :template => "weblog/show"
# Renders the template with a local variable
render :template => "weblog/show", :locals => {:customer => Customer.new}
Rails::Initializer.run do |config|
config.time_zone = "UTC"
end