Your SlideShare is downloading. ×
Rails Best Practices
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.

Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Rails Best Practices


Published on

Rails Best Practices …

Rails Best Practices
Characteristics of good code
1) Maintainability
2) Readability
3) Flexibility
4) Easy Delegation

Published in: Software
  • Be the first to comment

  • Be the first to like this

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

No notes for slide


  • 1. Rails Best Practices
  • 2. What’s Good Code? Good code is like a Good Joke!! It needs no explanation!!
  • 3. How to write Good Code? Follow Best Practices Characteristics of a good code:  Maintainability  Readability  Flexibility  Easy Delegation
  • 4. Fat Model, Skinny Controller
  • 5. SQL Injection  Prevent SQL injection by using ? to set the params of the query  Bad Practice  Good Practice
  • 6. N + 1 queries problem Consider 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)
  • 7. 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.
  • 8. Use Scopes  Bad Practice  Good Practice
  • 9. Use Query Attribute  Do 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?
  • 10. Annotate your models  Don’t go to schema.rb file everytime to find the table structure information.  We have a easier way. Use gem annotate  It will automatically add comments at the top and bottom of your models to list the table structures information
  • 11. Use model association Good Practice Bad Practice:
  • 12. Protect Mass Assignment Problem : Solution: You can use attr_protected to declare a blacklist of variables you don’t want it to be assigned
  • 13. DRY Code  Don’t repeat the code in controller. You can avoid that using before_filter  Eg: Bad practice
  • 14. Removing repeated code  As you saw in previous slide, below statement was repeated in all actions. We can avoid it if we use before filter  Good Practice
  • 15. References    Gem for code metric tool for rails-projects  Online service to find security issues in rails project 
  • 16. Thank You Want to join our team. Email your CV at Want to hire us. Contact us on