Justin Weiss's Blog, page 4
March 28, 2015
Go beyond the easy fix with code archaeology
When you go bugfixing, the quick, obvious change isn’t always the best one. And the code in front of you is never the whole story. To go beyond the easy fix, you have to know why certain decisions were made. You have to understand the history behind the code. And there are three great ways to learn what you need to know to confidently change code.
git blameWith the help of git blame, you can trace through every version of every line of code in a project, all the way back to when it was writt...
March 22, 2015
3 ways to monkey-patch without making a mess
Monkey Patching. When you first try Ruby, it’s amazing. You can add methods right to core classes! You don’t have to call Time.now.advance(days: -1), you can write 1.day.ago! It makes Ruby a joy to read and write. Until…
You hit weird bugs because a patch changed Hash.
You get confused about which code actually ran, so you can’t debug it when it breaks.
And you finally figure out that all your problems were caused six months ago, when you monkey-patched Enumerable to make one line of code fiv...
How to upgrade to Rails 4.2
Upgrading some Rails apps is as easy as bundle update rails.
But what if you have one of the other kind of apps? The ones that are still on Rails 4.0, or even 3.2, because you didn’t feel like dragging them kicking and screaming into the future?
Whether you’re facing a quick upgrade or a painful one, these steps will help you get your app to Rails 4.2 as smoothly as possible. And in the process, you’ll learn how to take full advantage of Rails 4.2’s new features.
Read the upgrade guideYou sh...
Finding your way around a new Rails project
You just changed teams at work, or just started a new job. Or you found a bug in your favorite open-source app, and want to write your first pull request. But after you git clone and open app/models, you’re totally lost. Even though the Rails structure is the same as what you’re used to, it’s impossible to find your way around the code. So what’s the fastest way to learn this new, unfamiliar Rails app?
Build a project vocabularyWhat is a Player, and what’s a Session? How about an Account vs...
How to keep Rails questions from killing your productivity
Practicing Rails is out! Get it for 25% off until Friday, February 13th.
While you write code, questions pop into your head constantly: How do I call this method? What options does it take? What happens if I pass an object instead of a number?
These questions destroy your productivity. When you don’t know the answer right away, you forget what you were doing. You get yanked out of your flow, and it takes a half hour to get back to where you left off.
So how do you find the answers you need wi...
Practicing Rails is now available!
Practicing Rails, my book on learning Rails without being overwhelmed, is now available. And you can still get 25% off until 11:59 PM Thursday night! (Pacific time)
Pick it up here: https://www.justinweiss.com/practicing-rails
A little background:
While I’ve written here, I’ve heard from so many people who have gone through introductory Rails books and videos, have learned the different parts of Rails, but still can’t get beyond the tutorial Rails apps.
They heard it was supposed to be fun, b...
A guide to extracting your first Ruby Gem
Is your GitHub contribution chart solid gray? You could use an open source project to work on. But you don’t have to start it from scratch. The easiest way to create a useful side project is to pull it out of the app you’re already building. That’s how Rails was born!
But how do you know what to extract? And how do you turn it into a gem, without destroying your workflow?
Find the code you want to extract.Somewhere, deep inside your app, is some code that doesn’t belong there. Code that does...
How to configure your Rails app to ship
You’re ready to launch your first production app, and it’s time to get it talking to some external services. You still have to get everything hooked up. So what’s the best way to configure your services in production, without making things more complicated on your dev machine?
Set up your EnvironmentTo configure production apps, today’s best practice is to use environment variables (those ENV["REDIS_HOST"]-looking things).
But why?
It’s harder to accidentally commit your production keys.
If...
A faster way to cache complicated data models
When your data model gets complicated, and your APIs hit that sad 1 second response time, there’s usually an easy fix: :includes. When you preload your model’s associations, you won’t make as many SQL calls. And that can save you a ton of time.
But then your site slows down again, and you think about caching responses. And now you have a problem. Because if you want to get responses from the cache:
1 2 3 4 results = {lawyer_1: 1, lawyer_2: 2, lawyer_3: 3} cached_objects = Rails.cache.fetch_mu...A couple of callback gotchas (and a Rails 5 fix)
ActiveRecord callbacks are an easy way to run code during the different stages of your model’s life.
For example, say you have a Q&A site, and you want to be able to search through all the questions. Every time you make a change to a question, you’ll want to index it in something like ElasticSearch. Indexing takes a while and isn’t urgent, so you’ll do it in the background with Sidekiq.
This seems like the perfect time to use an after_save callback! So in your model, you’ll write something li...


