Top Banner
17

Rails Best Practices

Jun 28, 2015

Download

Software

paramisoft

Rails Best Practices
Characteristics of good code
1) Maintainability
2) Readability
3) Flexibility
4) Easy Delegation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Rails Best Practices
Page 2: Rails Best Practices

Rails Best Practices

Page 3: Rails Best Practices

What’s Good Code?

Good code is like a Good Joke!!

It needs no explanation!!

Page 4: Rails Best Practices

How to write Good Code?

Follow Best Practices

Characteristics of a good code:

Maintainability

Readability

Flexibility

Easy Delegation

Page 5: Rails Best Practices

Fat Model, Skinny Controller

Page 6: Rails Best Practices

SQL InjectionPrevent SQL injection by using ? to set the

params of the query

Bad Practice

Good Practice

Page 7: Rails Best Practices

N + 1 queries problemConsider the following code which finds 10 clients and prints their postcodes

Total queries executed here are 11. 1 to fine 10clients and then 10(1 per each client to find its address)

Page 8: Rails Best Practices

Solution to N+1 queries problem

Eager Loading

Use “includes”. With includes, ActiveRecord ensures that all of the specified associations are loaded using minimum possible queries.

Page 9: Rails Best Practices

Use ScopesBad Practice

Good Practice

Page 10: Rails Best Practices

Use Query AttributeDo you always check if ActiveRecord’s attributes

exists or are blank? present?

Rails provides a cleaner way by query attribute

Bad Practice

Good Practice

Each attribute of ActiveRecord’s model has a query method, so you don’t need to use present? blank?

Page 11: Rails Best Practices

Annotate your modelsDon’t go to schema.rb file everytime to find the

table structure information.

We have a easier way. Use gem annotate https://github.com/ctran/annotate_models

It will automatically add comments at the top and bottom of your models to list the table structures information

Page 12: Rails Best Practices

Use model association

Good Practice

Bad Practice:

Page 13: Rails Best Practices

Protect Mass Assignment

Problem :

Solution: You can use attr_protected to declare a blacklist of variables you don’t want it to be assigned

Page 14: Rails Best Practices

DRY CodeDon’t repeat the code in controller. You can

avoid that using before_filter

Eg: Bad practice

Page 15: Rails Best Practices

Removing repeated codeAs you saw in previous slide, below statement

was repeated in all actions. We can avoid it if we use before filter

Good Practice

Page 16: Rails Best Practices

Referenceshttp://guides.rubyonrails.org/

http://rails-bestpractices.com/

Gem for code metric tool for rails-projects https://github.com/railsbp/rails_best_practices

Online service to find security issues in rails project http://rails-brakeman.com/

https://github.com/presidentbeef/brakeman