rails best practices
DESCRIPTION
Rails Best Practices Characteristics of good code 1) Maintainability 2) Readability 3) Flexibility 4) Easy DelegationTRANSCRIPT
![Page 1: Rails Best Practices](https://reader034.vdocument.in/reader034/viewer/2022042715/558fc44d1a28ab88318b45f5/html5/thumbnails/1.jpg)
![Page 2: Rails Best Practices](https://reader034.vdocument.in/reader034/viewer/2022042715/558fc44d1a28ab88318b45f5/html5/thumbnails/2.jpg)
Rails Best Practices
![Page 3: Rails Best Practices](https://reader034.vdocument.in/reader034/viewer/2022042715/558fc44d1a28ab88318b45f5/html5/thumbnails/3.jpg)
What’s Good Code?
Good code is like a Good Joke!!
It needs no explanation!!
![Page 4: Rails Best Practices](https://reader034.vdocument.in/reader034/viewer/2022042715/558fc44d1a28ab88318b45f5/html5/thumbnails/4.jpg)
How to write Good Code?
Follow Best Practices
Characteristics of a good code:
Maintainability
Readability
Flexibility
Easy Delegation
![Page 5: Rails Best Practices](https://reader034.vdocument.in/reader034/viewer/2022042715/558fc44d1a28ab88318b45f5/html5/thumbnails/5.jpg)
Fat Model, Skinny Controller
![Page 6: Rails Best Practices](https://reader034.vdocument.in/reader034/viewer/2022042715/558fc44d1a28ab88318b45f5/html5/thumbnails/6.jpg)
SQL InjectionPrevent SQL injection by using ? to set the
params of the query
Bad Practice
Good Practice
![Page 7: Rails Best Practices](https://reader034.vdocument.in/reader034/viewer/2022042715/558fc44d1a28ab88318b45f5/html5/thumbnails/7.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042715/558fc44d1a28ab88318b45f5/html5/thumbnails/8.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042715/558fc44d1a28ab88318b45f5/html5/thumbnails/9.jpg)
Use ScopesBad Practice
Good Practice
![Page 10: Rails Best Practices](https://reader034.vdocument.in/reader034/viewer/2022042715/558fc44d1a28ab88318b45f5/html5/thumbnails/10.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042715/558fc44d1a28ab88318b45f5/html5/thumbnails/11.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042715/558fc44d1a28ab88318b45f5/html5/thumbnails/12.jpg)
Use model association
Good Practice
Bad Practice:
![Page 13: Rails Best Practices](https://reader034.vdocument.in/reader034/viewer/2022042715/558fc44d1a28ab88318b45f5/html5/thumbnails/13.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042715/558fc44d1a28ab88318b45f5/html5/thumbnails/14.jpg)
DRY CodeDon’t repeat the code in controller. You can
avoid that using before_filter
Eg: Bad practice
![Page 15: Rails Best Practices](https://reader034.vdocument.in/reader034/viewer/2022042715/558fc44d1a28ab88318b45f5/html5/thumbnails/15.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042715/558fc44d1a28ab88318b45f5/html5/thumbnails/16.jpg)
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
![Page 17: Rails Best Practices](https://reader034.vdocument.in/reader034/viewer/2022042715/558fc44d1a28ab88318b45f5/html5/thumbnails/17.jpg)
Thank You
Want to join our team.Email your CV [email protected]
Want to hire us. Contact us on [email protected]