A Few Quick References For the Ruby Newbie Becoming Acquainted with Rails

Colin Schlecht
5 min readFeb 14, 2021

When learning Ruby on Rails there are many realizations that a newbie such as myself comes to. The first of which is that the backend isn’t quite as inaccessible as one might think, and that you won’t be spending countless coding hours to get it set up. (Not with ruby on rails at least). While there is much complexity in the backend, the brilliant minds who came before us wanted to make things easier for us all through the powerful tools baked into frameworks like ruby on rails.

Rather than having your hours spent building out the entire backend yourself — the countless hours will be spent tracking down typos, naming convention mistakes, and just making sure that your database is kept up to date with any structural changes.

This second realization came the hard way. Through learning that while there is less work than you may have anticipated, this isn’t exactly accomplished by magic. And this, is why you just spent all those long hours tracking down every place with an incorrect pluralization, spelling error, etc.

As intuitive as rails can be, and as helpful as active record associations are, the best way to save hours of headache is by ensuring you are working with the best practices outlined by the brilliant developers who came before you!

This post aims to provide few helpful reminders and cheats for those in the early stages of learning Rails. Keep in mind, that most of what information is being shared here is available in the documentation. Any time you’re working on a rails project, be sure to have a couple browser tabs pulled up with this documentation, or at least have it bookmarked for easy access!

Once you’ve mapped out your project and have a good idea of what your models will look like and how they’re related, you’ll need to begin creating your database. One of the main things that tripped me up when learning rails was knowing when to pluralize. Here’s a little table that will help:

Not sure if your word is plural, or how to pluralize it? Hit ‘rails c’ in the terminal to open up your console, type your word as a string, and append ‘.pluralize’ to the end to get a pluralized result!

Example:

It has to be magic!

These will come in handy to keep in mind when naming files if creating them manually, or if trying to remember whether or not a model needed to be plural when defining a class vs creating a table.

If you get tired of manually creating models and controller files, you can make use of the command line to speed up your back end building process!

By calling rails “generate”, or rails “g” for short, you can quickly add files without adding too much bloat to your projects, and quickly add resources to your project with minimal brain and finger usage!

Song (table name), name:string (column name, datatype), year_released:integer (another column name and datatype).

This will generate the following migration file, which will need to be migrated by either running rails db:migrate, or rake db:migrate, in order for it to be fully added to your database’s schema.

Could this be the migration file? How’d that get there, magic??

Notice that Rails will intuitively pluralize, punctuate, and case for you in many places as needed when using generators.

This is what your model might look like before adding all your fun associations, validations, methods, etc.

Here are some other basic examples of some powerful “rails g” commands that may come in handy.

Another helpful trick to remember is appending “references” to your migrations. This will add a migration to a table that generates foreign keys, and is useful when establishing associations between models.

Here is what the corresponding migration file might look like:

A word to the wise on taking shortcuts:

This is where the “rails magic” and mystery can start to really backfire. Always be sure to make use of your version control system, and commit often so that you can easily revert to a more stable build of your project. Because when making changes by migrations and other rails magic, you’re setting yourself up for bugs, even if you’re using best practices with your naming conventions.

Say for example, you run a migration and don’t catch a spelling error. That spelling error can find its way in to multiple files, which if not caught right away might be harder and harder to find as time goes by.

You also want to be sure that you are maintaining your database schema to match any associations you’ve established. For example, for any belongs to associations you MUST create the proper foreign keys. And for the “reference” example above, don’t forget to double check that your models have the appropriate associations written in, with proper pluralization.

But if you’re like me, and learn most from the long hours of just playing around, don’t be afraid to get your hands dirty!

Familiarize yourself with the errors early in order to get a better working relationship with rails.

--

--