SlideShare a Scribd company logo
Rails Best Practices
By
Nitesh Varma
Developer
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.
Rails best practices
Rails best practices
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.
Rails best practices
Questions?
Thank you

More Related Content

PDF
Rails Tips and Best Practices
PDF
Dynamically Composing Collection Operations through Collection Promises
PDF
Reflection in Pharo: Beyond Smalltak
PDF
Styled Components & React.js
PDF
Variables in Pharo5
PDF
Reflection in Pharo: Beyond Smalltak
PDF
Introduction repository, ddd and unit test
PDF
Actor model : A Different Concurrency Approach
Rails Tips and Best Practices
Dynamically Composing Collection Operations through Collection Promises
Reflection in Pharo: Beyond Smalltak
Styled Components & React.js
Variables in Pharo5
Reflection in Pharo: Beyond Smalltak
Introduction repository, ddd and unit test
Actor model : A Different Concurrency Approach

What's hot (13)

PDF
Reflection in Pharo5
PPTX
Js tips & tricks
PPTX
Akka framework
PDF
Akka - A Brief Intro
PDF
Metaprograming rails magic
PPTX
Javascript - Arrays - mutator functions
PPTX
AngularJS best practices
PDF
Javascript classes and scoping
PDF
Serialization
PDF
Introduction to the Actor Model
PPTX
Introduction to Python Programming
PDF
Intro to javascript (5:2)
PPTX
Relay: Seamless Syncing for React (VanJS)
Reflection in Pharo5
Js tips & tricks
Akka framework
Akka - A Brief Intro
Metaprograming rails magic
Javascript - Arrays - mutator functions
AngularJS best practices
Javascript classes and scoping
Serialization
Introduction to the Actor Model
Introduction to Python Programming
Intro to javascript (5:2)
Relay: Seamless Syncing for React (VanJS)
Ad

Viewers also liked (6)

PDF
Ride on the Fast Track of Web with Ruby on Rails- Part 2
PPTX
Clean Code
PPTX
Presentation
PDF
Metaprogramming in C++ - from 70's to C++17
PDF
Metaprogramming
PPTX
Creating an Adaptive Setting
Ride on the Fast Track of Web with Ruby on Rails- Part 2
Clean Code
Presentation
Metaprogramming in C++ - from 70's to C++17
Metaprogramming
Creating an Adaptive Setting
Ad

Similar to Rails best practices (20)

PDF
How can JAVA Performance tuning speed up applications.pdf
PDF
Top 7 Angular Best Practices to Organize Your Angular App
PDF
AliExpress’ Way to Microservices - microXchg 2017
PPTX
Learning Web Development with Ruby on Rails Launch
PDF
Building APIs using Laravel - A simple approach to scale
PDF
React Native App Development.
PPTX
Rapid Application Development with MEAN Stack
PPTX
Comprehensive Guide to React Development 2022.pptx
PDF
Managing Large Flask Applications On Google App Engine (GAE)
PDF
How to implement ruby on rails testing practices to build a successful web ap...
PPTX
All about that reactive ui
PDF
Beyond rails new
PPTX
A Beard, An App, A Blender
PDF
System design for Web Application
DOC
Satish resume
PPTX
Top 3 Mistakes when Building
PDF
Using MLOps to Bring ML to Production/The Promise of MLOps
PPTX
Orchestration, the conductor's score
PDF
Web Application Development – Top 8 Frameworks in 2025.pdf
PDF
Building multi billion ( dollars, users, documents ) search engines on open ...
How can JAVA Performance tuning speed up applications.pdf
Top 7 Angular Best Practices to Organize Your Angular App
AliExpress’ Way to Microservices - microXchg 2017
Learning Web Development with Ruby on Rails Launch
Building APIs using Laravel - A simple approach to scale
React Native App Development.
Rapid Application Development with MEAN Stack
Comprehensive Guide to React Development 2022.pptx
Managing Large Flask Applications On Google App Engine (GAE)
How to implement ruby on rails testing practices to build a successful web ap...
All about that reactive ui
Beyond rails new
A Beard, An App, A Blender
System design for Web Application
Satish resume
Top 3 Mistakes when Building
Using MLOps to Bring ML to Production/The Promise of MLOps
Orchestration, the conductor's score
Web Application Development – Top 8 Frameworks in 2025.pdf
Building multi billion ( dollars, users, documents ) search engines on open ...

More from Achintya Kumar (6)

PDF
Clean code
PPTX
Speeding up web_application
PPTX
Introduction to-angular js
PPTX
Application lifecycle management
PPTX
Code Refactoring using rails
PPTX
Graph database
Clean code
Speeding up web_application
Introduction to-angular js
Application lifecycle management
Code Refactoring using rails
Graph database

Recently uploaded (20)

PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Electronic commerce courselecture one. Pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPT
Teaching material agriculture food technology
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Network Security Unit 5.pdf for BCA BBA.
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
“AI and Expert System Decision Support & Business Intelligence Systems”
Per capita expenditure prediction using model stacking based on satellite ima...
Review of recent advances in non-invasive hemoglobin estimation
Programs and apps: productivity, graphics, security and other tools
sap open course for s4hana steps from ECC to s4
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Encapsulation_ Review paper, used for researhc scholars
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
Electronic commerce courselecture one. Pdf
Advanced methodologies resolving dimensionality complications for autism neur...
Teaching material agriculture food technology
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Network Security Unit 5.pdf for BCA BBA.
The AUB Centre for AI in Media Proposal.docx
MYSQL Presentation for SQL database connectivity
Mobile App Security Testing_ A Comprehensive Guide.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx

Rails best practices

  • 1. Rails Best Practices By Nitesh Varma Developer Allerin Tech Pvt Ltd
  • 2. 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.
  • 3. 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.
  • 4. • Below are the some best practices in Rails , which should have to be considered at the time of development when using Rails framework.
  • 5. 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.
  • 8. 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
  • 9. If we do not use Exception type qualifier, then Ruby will catch only StandardError, which is probably what we want from begin-rescue:
  • 10. 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:
  • 11. 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:
  • 12. Caching with instance variable or Use memoization
  • 13. How can we avoid the duplicated db query and duplicated calculation? We can use following methods.
  • 14. We can also do this with memoize
  • 15. 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.
  • 16. 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.
  • 17. find_in_batches is similar to find_each except that it yields the array of objects
  • 18. 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.