DEV Community

Cameron Youngblood
Cameron Youngblood

Posted on

Rake | Junior Rails Review

Hi, my name is Cameron Youngblood 👋🤠

I write about the experience of learning Ruby on Rails from a beginner's perspective. New article each week.

I'm currently looking for a junior Rails position!

Follow me or shoot me a DM on Twitter → @youngbloodcyb

My Experience With Rake

Not long ago, I was playing around with a Rails project that I cloned from a repo on GitHub. I’m a little embarrassed to say that it was actually my first time cloning someone else’s Rails project and running it locally. As I set it up to run on my machine, unsurprisingly, I ran into a few issues. The first one was related to some necessary Postgres configuration. Up to that point, I had primarily used the default SQLite for my local projects, so it felt like stepping into a new world. While it was initially frustrating to work through, I eventually figured out the issue and learned a ton.

With Postgres finally running, I started up the rails server again and moved on to my next error: ActiveRecord::ConnectionNotEstablished. After a few Google searches and lots of time, I came to the realization that I failed to setup the database and migrate the schema. On a StackOverflow thread, I read one way to resolve this was to run rake db:setup and rake db:migrate. I had used rails db:migrate before, but this “rake” word was new to me.

“Ok, whatever,” I thought. It didn’t matter that I didn’t know what it meant. I copied and pasted those two lines to the command line, restarted the server and bam, it worked. However, I still had no idea what exactly happened. My app was running, but I didn’t know what “rake” did, and that really bothered me.

Based on my experience, albeit limited, I knew generally what db:setup and db:migrate did. However, I didn’t really know what the word rake meant. I knew I had recognized it somewhere… “Where have I see this before? Oh, right!” There’s a Rakefile in the root directory of every Rails app I’ve interacted with. Naturally I thought, “Maybe there will be a hint in there as to what it does.”

In the default Rakefile there’s a comment that says, “Add your own tasks in files placed in lib/tasks ending in .rake.” So, I thought, “Oh… so is rake kind of like npm scripts that I can write and run for a rails project?” I followed the instructions in the Rakefile to the lib/tasks directory to see if I could find definitions for the rake tasks that I used previously to magically fix my Rails app. I assumed maybe there were pre-defined methods such as db:migrate like I used before. However, the directory was empty. So, instead, I backtracked and followed the Rakefile to the config/application directory. There was definitely more in there, but I sadly couldn’t grok anything from this file relating directly to rake (at least as far as I could tell).

At this point, the only thing I could think was, “Well, if all applications I’ve seen have Rakefile in them, then rake must be something that comes default with Rails. So, there must be something in the Rails Guides that explains what rake is.”

But I was wrong. I went to the Rails guides and searched for the work “rake” on every page I could find. While there was an occasional instance of the word, there wasn’t any explanation of what it did. Instead, the best explanation I could find was “Active Record supports using rake.” Well, I kind of figured that much, but that didn't tell me what I wanted to know.

After some more digging, I came across the docs on GitHub for the rake gem itself. In an attempt to understand the commands I executed earlier (rake db:setup and rake db:migrate), I skimmed the glossary and command_line_usage files in the docs. But, to no avail, I couldn’t find anything about running database migrations using rake.

Finally, after a while of perusing through Google search results without finding a clear answer, I found an informative blog article on Medium from Emir Vatric. In it, he goes into some helpful detail about what rake can do and how to create rake tasks. But most helpful to me was a simple distinction between rake and rails:

Worth noting is that since Rails 5 core team decided to have consistency by enabling rails command to support everything that rake does. Some favor using rake over rails.

While this didn’t clear up all the questions I had about rake, it definitely helped me understand why I would often see the word rake instead of rails. Additionally, it helped me understand why the code I copied from StackOverflow ran without any issues.

For experienced developers who have used Rails for many years, I bet it’s almost laughable to see such a simple thing completely stump someone. However, for me, a newcomer to the Rails ecosystem, it was extremely confusing. In fact, I still have so many questions. For example, why don’t I need the rake gem to be installed to be able to use the command rake db:migrate? Is it better practice to use rails db:migrate, or rake db:migrate? Is it just preference? Do each perform the exact same actions? Are they actually the same thing, or am I just misunderstanding?

Conclusion & Recommendation

Overall, my confusion on rake was not a devastating roadblock to getting an app up and running. More than anything, it was just an inconvenience. But, I would venture to say that other people learning Ruby on Rails have probably wondered or been stumped by the same thing.

While one of the sources of my problem was likely me looking at outdated StackOverflow threads, I think there could be more clarification in the Rails docs addressing what rake and Rakefile are. As a beginner, I felt like I was going on a wild goose chase to understand what they meant. In the end, I learned a lot that I wouldn't have known otherwise. My only wish is that correct information would have been easier to find and readily available at a reliable source.

Thanks for reading this first post from Junior Rails Review! Feel free to follow me on Twitter. Additionally, if there was something I was missing in this post or something that was inaccurate, please reach out!

Top comments (0)