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

119

Published on

Rails Best Practices …

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

Published in: Software
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
119
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 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 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
  • 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  http://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
  • 16. Thank You Want to join our team. Email your CV at hr@paramisoft.com Want to hire us. Contact us on engage@paramisoft.com

×