db:fresh - Easily rebuild your DB from scratch
I suppose the idea behind Rails’ migration is that once you create them, you don’t change them. This is usually the right concept, and from this decision grows many of the rake db:* tasks. Such tasks — migrate, reset, schema:load, schema:dump, and setup — do not re-run migrations as they assume schema.rb is up to date (rightfully so). But oftentimes (especially on greenfield development) I will tweak my migrations and want to start from scratch. So here’s a quick hitter I now have my application templates add to every project I start:
module DBTasks
namespace :db do
desc "drops, creates, migrates and seeds the DB"
task :fresh => :environment do
puts "Dropping DB..."
Rake::Task["db:drop"].invoke; puts "done."
puts "Creating DB..."
Rake::Task["db:create"].invoke; puts "done."
puts "Migrating DB..."
Rake::Task["db:migrate"].invoke; puts "done."
puts "Seeding DB..."
Rake::Task["db:seed"].invoke; puts "done."
puts "Your DB is so fresh and so clean clean!"
end
end
end
How To Improve bit.ly 200%
If I wanted to tweet about a silly magnifier glass iPhone application, today I could tweet
What a pointless iPhone app http://bit.ly/172F9b !
But that uses too many characters just on the shortened URL without expressing anything! We should be succinct and stop wasting random letters on bit.ly’s unique id:
What a http://bit.ly/pointless_iPhone_app!
Now that’s a nice (and short!) URL. bit.ly is 6 characters. The unique id they use (in this example 172F9b) is also 6 characters. By dumping the unique id entirely and allowing custom text in the URL, bit.ly would waste only half as many characters and become an integral part of the message.
1_000_000 Ruby’s
Ruby ignores the underscore character within integer literals. Instead of writing
bank_account.deposit(:cash => 1000000)
you can write
bank_account.deposit(:cash => 1_000_000)
which is much easier to read.
When I’m depositing a million at my ATM however, I will a) refactor that constant out of my code and b) probably be blogging from somewhere other than my bedroom/home office =)
Ruby 1.9 fluent API
I don’t like magic because I don’t understand how it works. After I understand it, it’s no longer magic, but I usually have a much greater appreciation for the execution.
I’ve been using Ruby without understanding the language’s nuts and bolts for a while. So I picked up O’Reilly’s The Ruby Programming Language and am making my way through it. Along the way I will document some of the useful and interesting pieces that I enjoy…
A ‘fluent API’ allows coders to conveniently chain many statements together which act on an object (or a series of objects there derived) in a logically progressive manner.
indy500_winner = Race.new('Indianapolis').start.run_laps(500).winner
While elegant, these chains can get very long. Ruby 1.9 has altered its statement terminator rules to allow continuations on successive lines. When the first non-whitespace character on a line is a period, it is considered a continuation.
indy500_winner = Race.new('Indianapolis')
.start
.run_laps(250)
.yellow_flag(10)
.run_laps(240)
.winner
Nice, eh?
Even more interestingly, my middle name is John.
Dan - you making some money on the side? Or atleast trying to? I found this on a street light while walking home downtown. Overtime’s CTO is Dan Spinosa… possible alias?
You make a product for a penny, you sell it for a dollar and you sell it to addicts.
– Warren Buffet (on Big Tobacco)Whenever: A Hidden Gem
After learning cron syntax, figuring out what the PATH is during cron execution and how to set it in my crontab, and documenting my crontab within my application, I discovered Whenever. Javan Makhmali’s Whenever gem allows you to write self-documenting ruby in your application to define your cron jobs, then integrates beautifully with Capistrano to update your crontab on deploy. It also saves me from having to lookup cron syntax every few months when I need to edit something (and have, of course, forgotten the syntax).
The documentation for Whenever is very good, and there’s even a Railscast that provides a step by step. But there is one setting not mentioned anywhere, which I only found digging through the code:
set :set_path_automatically, false
Nominally, Whenever writes a PATH=... line before it lists your cron jobs. This is fine most of the time, but on some systems (like Joyent’s SunOS 5.11) the extended Vixie Cron syntax (the standard used on most *nix systems) is not supported. As such, you may see an error like
crontab: error on previous line; unexpected character found in line.
if PATH is set in your crontab. So, disable it in your schedule.rb and get back to real work.
p.s. Will Tumblr ever create a post type for code?
First Post, No Pressure.
I’ve known Joe for two decades now. But I’ve watched him grow (up) for the last 4 years. I’m pleased to present my friend and business partner, J.Y.
Via Joe's DevelopmentJust trying to start this off, so I’ll post a quick message. While I was staying at Reece Pacheco’s (Overtime Media CEO) house in Cape Cod for a few weeks in July, I was made aware of something about myself. The Pacheco’s do a wide variety of things I’ve never done before. Be it the food they eat, the way they cook, the activities they participate in, etc.
Reece made me aware of the fact that I, for the most part, say “yes” to everything with little hesitation.
Reece: ”Joe do you want dried cranberries in your salad?”
Joe: ”Well, I’ve never had any before… so yes.”
Reece: ”Wanna try paddle boarding?”
Joe: ”Never heard of it… absolutely.”
Reece: ”Hey I’m gonna run a triathlon, wanna join?”
Joe: (I can barely swim) “Yea, definitely.”
My point being, one of the best way to develop yourself is to have a wealth of experiences. You can’t experience things if you don’t learn to say “yes” more often than not. If I can quote Jay-Z (this songs playing on my iTunes right now):
“I’m not afraid of dying, I’m afraid of not trying. Everyday, hit every wave like I’m Hawaiian.”
I learned this a little later than most, but hey, better late then never.
Rails Session Domain
Rails’ environment.rb (like most of the generated files) has a lot of comments that help you learn rails and build your app. Until yesterday I never used the :domain key of the action_controller.session, and I’m somewhat surprised Rails’ make no mention of it in environment.rb. But I have been using Rails for almost 2 years now, so I suppose it’s not in there because you can get by without it for quite some time =)
After upgrading our SSL certificate to use a wildcard, I directed secure pages to the secure subdomain (i.e. https://secure.SportKong.com) to really drive home the point that it’s secure. I then directed non-secure pages to the regular domain (http://SportKong.com) but the users’ session was lost. I needed to set config.action_controller.session[:domain] = ".sportkong.com" so that sessions would span across multiple domains.
