Things I Couldn’t Figure Out Then Figured Out in Ruby On Rails — Part 1
I went to a Ruby hacknight the other night! There, I spent an hour and a half pair-debugging what turned out to be a painfully simple issue.
The issue I was having:
I needed to generate a migration: rails generate migration add_product_url_to_wishlists
. But when I would type that into the terminal, I would get the following error:
Other behavior:
rails console
produced the same error as thegenerate migration
command.
But:
rails server
ran successfully.
I should mention that my view of the error message was not as it looks above. I have, before now, kept the terminal text large and more readable, which makes the error message broken up and scrollable and perhaps… less readable. I think I may have seen it if I’d looked at it as it’s pictured above. But who knows?!
Here’s what I did to try and fix it:
- Made sure I was using the correct command to generate the migration. I was! Yay.
- Stopped/started the server.
- Ran the
migration
command withbundle exec
. - Checked my other Rails projects to see if they were having errors. They were not.
- Googled the first couple lines of the error message, which lead me to:
— Comment out bootsnap
in my Gemfile
. Then uncomment it out.
— Add config.autoload_paths += %W(#{config.root}/lib)
to the DressMe
module in application.rb
. Then delete that line.
Last ditch efforts, I:
- Cleared the cache.
- Bundled.
- Bundle updated.
Lastest ditchest of efforts, I:
- Restarted my computer.
After more googling with my pair, we:
- Did all the things I had previously done, to make sure nothing had fallen through the cracks (he was baffled that
rails server
worked, butrails console
did not. - Typed
which ruby
andwhich rails
in the command line to see if we were using the correct versions for the project. - Stopped Spring with the
stop spring
command. - My pair pulled down the repo and we both checked out very old commits* (He got the same errors that I was getting).
- More googling.
- More cache clearing.
Then we talked about the most recent additions I’d made to the app. I had added the auto-links
gem a couple days before. It allows urls to be clickable. So, we did a bit more googling and error message reading with auto-links
in mind. And then the answer revealed itself.
The error was occurring because… the first few lines of my application.rb
file looked like this:
Not like this:
The error was occurring because Ruby was trying to load the rails_autolink
gem, which depended on Rails, which hadn’t been loaded yet.
This was fun. I learned things! And we solved the problem together!
- I learned that I’d like to keep my terminal text size a bit smaller, so I can read the whole error message at once without scrolling.
- I learned that reading the whole error message can help figure out the… error.
- I learned that the
require
order is important inapplication.rb
. - I learned how to clear my cache.
- I learned how to stop Spring, and why we use it or need it.
*It doesn’t make sense to me that even with older commits we were getting the same errors, if I’d added this Gem and line of code only a few days before. If anyone has info about why that would be, please do tell.