Rails Best Practices
ByNitesh VarmaDeveloper Allerin Tech Pvt Ltd
What is best practices?
• Best practices are using approaches that not only delivers superior results but also consider feasibility and ongoing development of the approach.
Need?• With the fast pace of today’s agile development
industries, we know how important it is to complete a project on time.
• We also recognize the importance of other factors such as flexibility, readability & most important performance of the application.
• Even experienced developers some time do not consider above points at the initial stage of application launch, but later it start deteriorating the performance when the data’s in the application grows exponentially and enhancement needs to be done in existing application.
• Below are the some best practices in Rails , which should have to be considered at the time of development when using Rails framework.
Use Eager Loading (Prevent N + 1 query)
• Most of the time new Rails developers do not use eager loading of objects in Rails.
• Eager Loading is highly recommended at the time of development in Rails.
• It mainly resolved the common issues like N + 1 queries.
• We can detect the N + 1 queries issues by using gem ‘Bullet’ in development environment.
Don’t Rescue Exception, Rescue Standard Error
Explicitly rescuing Exception will also rescue code errors such as SyntaxError, LoadError etc.Consider below begin-rescue syntax
If we do not use Exception type qualifier, then Ruby will catch only StandardError, which is probably what we want from begin-rescue:
Use query attribute
We always check if ActiveRecord’s attributes exist or not by nil?, blank? or present? ? Rails provides a cleaner way for this by query attribute.Suppose locked is boolean value of users table.General:
The query attribute is almost the same as the present? method call on attribute, or the opposite of blank? method call. Each attribute of ActiveRecord’s model has a query method, so we don’t need to use the present? or blank? for ActiveRecord’s attributes.
Re-factored:
Caching with instance variable or Use memoization
How can we avoid the duplicated db query and duplicated
calculation? We can use following methods.
We can also do this with memoize
Use batched finder for large data query
Suppose you have social network website containing millions of user and you needs to send daily notification to them morning.
Using find_each, the application only finds 1000 users once, yield them, then handle
the next 1000 users, until the last 1000 users. That means the application will only
load 1000 user objects into memory each time.
1000 is the default batch size, if we want to change batch_size we can use :batch_size
option to change it.
find_in_batches is similar to find_each except that it yields the array of objects
Replace instance variable with local variable
Partial is a reusable view template, In partial view, we can use the instance variable directly, but it may be confused and make it hard to reuse anywhere (variable leakage), because we don’t know exactly which instance variable can be used, so we can use the local variable in partial which will be explicitly assignment at the time of render.
Questions?
Thank you