rails best practices
TRANSCRIPT
![Page 1: Rails best practices](https://reader036.vdocument.in/reader036/viewer/2022062712/55d58fabbb61eb3a418b462e/html5/thumbnails/1.jpg)
Rails Best Practices
ByNitesh VarmaDeveloper Allerin Tech Pvt Ltd
![Page 2: Rails best practices](https://reader036.vdocument.in/reader036/viewer/2022062712/55d58fabbb61eb3a418b462e/html5/thumbnails/2.jpg)
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.
![Page 3: Rails best practices](https://reader036.vdocument.in/reader036/viewer/2022062712/55d58fabbb61eb3a418b462e/html5/thumbnails/3.jpg)
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.
![Page 4: Rails best practices](https://reader036.vdocument.in/reader036/viewer/2022062712/55d58fabbb61eb3a418b462e/html5/thumbnails/4.jpg)
• Below are the some best practices in Rails , which should have to be considered at the time of development when using Rails framework.
![Page 5: Rails best practices](https://reader036.vdocument.in/reader036/viewer/2022062712/55d58fabbb61eb3a418b462e/html5/thumbnails/5.jpg)
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.
![Page 6: Rails best practices](https://reader036.vdocument.in/reader036/viewer/2022062712/55d58fabbb61eb3a418b462e/html5/thumbnails/6.jpg)
![Page 7: Rails best practices](https://reader036.vdocument.in/reader036/viewer/2022062712/55d58fabbb61eb3a418b462e/html5/thumbnails/7.jpg)
![Page 8: Rails best practices](https://reader036.vdocument.in/reader036/viewer/2022062712/55d58fabbb61eb3a418b462e/html5/thumbnails/8.jpg)
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
![Page 9: Rails best practices](https://reader036.vdocument.in/reader036/viewer/2022062712/55d58fabbb61eb3a418b462e/html5/thumbnails/9.jpg)
If we do not use Exception type qualifier, then Ruby will catch only StandardError, which is probably what we want from begin-rescue:
![Page 10: Rails best practices](https://reader036.vdocument.in/reader036/viewer/2022062712/55d58fabbb61eb3a418b462e/html5/thumbnails/10.jpg)
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:
![Page 11: Rails best practices](https://reader036.vdocument.in/reader036/viewer/2022062712/55d58fabbb61eb3a418b462e/html5/thumbnails/11.jpg)
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:
![Page 12: Rails best practices](https://reader036.vdocument.in/reader036/viewer/2022062712/55d58fabbb61eb3a418b462e/html5/thumbnails/12.jpg)
Caching with instance variable or Use memoization
![Page 13: Rails best practices](https://reader036.vdocument.in/reader036/viewer/2022062712/55d58fabbb61eb3a418b462e/html5/thumbnails/13.jpg)
How can we avoid the duplicated db query and duplicated
calculation? We can use following methods.
![Page 14: Rails best practices](https://reader036.vdocument.in/reader036/viewer/2022062712/55d58fabbb61eb3a418b462e/html5/thumbnails/14.jpg)
We can also do this with memoize
![Page 15: Rails best practices](https://reader036.vdocument.in/reader036/viewer/2022062712/55d58fabbb61eb3a418b462e/html5/thumbnails/15.jpg)
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.
![Page 16: Rails best practices](https://reader036.vdocument.in/reader036/viewer/2022062712/55d58fabbb61eb3a418b462e/html5/thumbnails/16.jpg)
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.
![Page 17: Rails best practices](https://reader036.vdocument.in/reader036/viewer/2022062712/55d58fabbb61eb3a418b462e/html5/thumbnails/17.jpg)
find_in_batches is similar to find_each except that it yields the array of objects
![Page 18: Rails best practices](https://reader036.vdocument.in/reader036/viewer/2022062712/55d58fabbb61eb3a418b462e/html5/thumbnails/18.jpg)
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.
![Page 19: Rails best practices](https://reader036.vdocument.in/reader036/viewer/2022062712/55d58fabbb61eb3a418b462e/html5/thumbnails/19.jpg)
![Page 20: Rails best practices](https://reader036.vdocument.in/reader036/viewer/2022062712/55d58fabbb61eb3a418b462e/html5/thumbnails/20.jpg)
Questions?
![Page 21: Rails best practices](https://reader036.vdocument.in/reader036/viewer/2022062712/55d58fabbb61eb3a418b462e/html5/thumbnails/21.jpg)
Thank you