Learn What it Takes to Be a Solid Rails Developer!
Tags: rails, beginners, cleancode, devjournal
Published: 2023-01-24 14:41:50

Unlock the full potential of Ruby on Rails and build web applications that will leave your peers in awe. But before you can do that, you need to master these essential concepts that are the building blocks of any successful Rails project. Let's dive in and explore them one by one

**1. Familiarize yourself with Active record association and its best practices**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z117oasdz5j8czu5tap5.png)
{% embed https://www.bigbinary.com/books/learn-rubyonrails-book/defining-associations-and-best-practices %}

I regret not finding this article when I was struggling to learn active record associations🥲(It's so good). This article will not only explain the various types of associations, but also provide a clear overview of how the associations interact with the database through the use of foreign key and primary key constructs.

**2. Design patterns are the MACHINE GUNS🚀 of your codebase**

 {% embed https://www.bacancytechnology.com/blog/design-patterns-in-ruby-on-rails %}

Clean code should never be underestimated, as it can save significant time for both yourself and other developers when debugging. Design patterns are the best practices followed by industry leaders. They not only make your code more readable (eg: Query objects) but also reduces code complexity(eg: Form objects) by separating the concerns (eg: Service object). Therefore, easy to maintain and review.

**3. Only Unit testing can save you from Production crashes🔥**

{% embed https://thoughtbot.com/blog/back-to-basics-writing-unit-tests-first %}

Testing your code with every single input under the sun after every tiny tweak is like a recurring nightmare, amirite? 😴. That's where unit testing comes into rescue. It's a smart way writing code, where you write tests first before you even write the code. Basically, you make changes to your code, run the tests and boom👊🏻!, all the failed tests show up on your screen. Check out the above article for more details.

{% embed https://www.guru99.com/code-coverage.html %}

Just writing test for the concerned parts of the code doesn't make your code production crash proof. Writing quality test that can provide maximum code coverage is what makes your code crash proof (99.9% times😅). The article above breaks down all the ways to make sure you're testing every nook and cranny of your code before it goes live. .

**4. Code optimization is our Ultimate Goal💪🏻**

There's a lot of things that can slow down our code execution, like using too many resources at the same time, bad design choices, and unoptimized code, but the one thing we can control is our code. So, let's make sure it's running as smooth as butter.
The following are the most common causes of poor application performance.

##### Slow view rendering :
Run any view of your rails application and check the logs in the terminal. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0iwu3l2etibdohea66bj.png)

You will be able to find a line that will let you know how long it took for your view to load, how long it took for ActiveRecord to fetch data from the database, and how much time was spent creating new objects. Slow view rendering can be caused by factors such as an excessive amount of logic in the view, a large number of partials, and unoptimized images and other assets.

{% embed https://teamhq.app/blog/tech/17-how-rendering-partials-can-slow-your-rails-app-to-a-crawl %}


If your view is taking forever to load and you can't figure out why, it might be a good idea to give the above article a quick read.



##### N+1 queries : 

A common performance issue in Rails is the N+1 query problem, where a separate database query is made for each item in a collection. This can cause a lot of unnecessary database queries and slow down the view rendering.
To understand what n+1 queries are in rails, you can refer the below article.
    {%embed https://dev.to/junko911/rails-n-1-queries-and-eager-loading-10eh %}

##### Large number of partials :
When you've got too many little chunks of views (partials) hanging around, it can slow down how fast your page loads. It's best to keep that number down, But If your existing application has too many partials which are not optimizable then its time to move to view_components. View components are a way to group common view logic together, which can simplify your code and make your application perform better.

If you're interested in experimenting with it, the article below can serve as a good starting point.

  {%embed https://www.honeybadger.io/blog/ruby-view-components/ %}

##### Lack of caching :
Rails, by default, does not include caching functionality. However, it does provide several options for caching through the use of third-party gems, such as the `dalli` gem for memcached caching and the `redis-rails` gem for Redis caching. Additionally, Rails also provides caching options through built-in methods such as `cache` and `expires_in`.

There are several gems available that can help identify and solve caching issues in Rails code. These gems can analyze your application and provide recommendations for optimizing caching strategies, such as identifying areas where caching can be added or improved. Some examples of such gems include `rails_best_practices`, `bullet`, and `rack-mini-profiler`.

If you're feeling like a mad scientist ready to conduct some experiments with these gems, the below post can be your lab coat and goggles😜.
{% embed https://medium.com/swlh/fix-it-until-you-make-it-a-simple-ruby-on-rails-performance-guide-4fa95b79df8 %}

All in all, Clean code practices for Ruby on Rails developer are a combination of using OOP principles, adhering to Rails conventions, automated testing, and keeping the codebase organized and well-commented.