Understanding The Magic Of Rails

Georgette Class-Peters
3 min readJan 18, 2021

I have been learning Ruby for the past few months. I can honestly say it’s been one of the hardest undertakings I have ever taken on. From what I have learned from Ruby so far it is a very useful and powerful programming language that can be used to build a fully functional application in minutes. This is especially true when speaking of Ruby on Rails. Initially, after working with Sinatra I was extremely excited to move on to Rails because I heard all the tales of Rails magic. I was quickly hit in the face with the downsides of having so much of the code abstracted away for you and having some of the mundane tasks done for you. I sometimes found it tough to truly understand what all the layers of Rails were doing at each time, understanding how Rails knew which views to map to implicitly, understanding how Rails knew where my submit button was supposed to go after my forms were filled out, etc. After struggling to understand the conventions of Rails for a while I took some time to genuinely understand what Rails was doing in a lot of cases. I would not give that extra time up for the world. I feel like taking the extra time really reinforced the Rails concepts in my head, improved my flow as a programmer, and helped me to better build out my applications as a whole.

Here are some concepts I was able to get a better understanding of how Routing is done differently in Rails vs Sinatra. The fact that I have been studying rails for the past couple of months causes me to look back and cringe at the fact that we were wiring restful routes in the controller instead of Config /Routes.rb. For me, the way we accomplish routing in Rails makes a lot more sense to me. The flow I used was to draw the route in the config file, map it to a controller action, build out the flow in the controller, create the instance variable, and put in the proper keys to create, save, update, show and delete your object and create the corresponding view. This flow and the existence of the routes.rb file frees up the controller to work on setting instance variables, performing CRUD actions, and Rendering views.

Scope methods are a way of querying data from the database. In order to get back specific information or to order your information in different ways, I implemented a scope method by using a search bar so that a user could search my database for specific songs.

I found Validations in Rails to be very similar to Sinatra. Validations are very important in an application especially one with forms so that you as a developer can receive information back from the user in the correct format you would like it to correctly save the information to the database.

Generators were a big New thing in Rails. A Generator is a feature built into Rails that write code and build file trees much based on single-line commands to your console. To me, they were super useful because it took away some of the mundane things you have to do over and over when building an application. Generators are used for things like creating blank migrations, generating resources, creating models, and building file trees for you.

In Terms of Association in Rails, I learn how to use has_many through relationships. This in turn taught me a lot about Join- tables and how associations work as well as some things you can do when associations are performed correctly.

Last but not least Omniauth. Omniauth is A gem for rails that allows you to use multiple authentication providers. It provides an interface to your application that abstracts the whole Oauth protocol logic from you. Your user signs in with Facebook, Google, Git-hub, or any other provider, to their account and gets a token. This token is bound to your user in your app, and omniauth uses this to identify your user and sign them in.

--

--