21
Rails Best Practices By Nitesh Varma Developer Allerin Tech Pvt Ltd

Rails best practices

Embed Size (px)

Citation preview

Page 1: Rails best practices

Rails Best Practices

ByNitesh VarmaDeveloper Allerin Tech Pvt Ltd

Page 2: Rails best practices

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

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

• 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

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
Page 7: Rails best practices
Page 8: 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

Page 9: Rails best practices

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

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

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

Caching with instance variable or Use memoization

Page 13: Rails best practices

How can we avoid the duplicated db query and duplicated

calculation? We can use following methods.

Page 14: Rails best practices

We can also do this with memoize

Page 15: Rails best practices

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

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

find_in_batches is similar to find_each except that it yields the array of objects

Page 18: Rails best practices

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
Page 20: Rails best practices

Questions?

Page 21: Rails best practices

Thank you