NASA666

不以物喜,不以己悲.
Posted on , viewed 650 times

Rails Best Practices

  1. Use Time.zone.now instead of Time.now
  2. Don’t modify the params hash
  3. default_scope is evil
  4. monitor your backend services
  5. Pay more attentions on security
  6. speed up assets precompile with turbo-sprockets-rails3
  7. Check the return value of “save”, otherwise use “save!”
  8. Don’t rescue Exception, rescue StandardError
  9. Tell, don’t ask
  10. split your cap tasks into different files
  11. rolling out with feature flags
  12. use after_commit
  13. Protect mass assignment
  14. Not use time_ago_in_words
  15. Name your model methods after their behavior, not implementation.
  16. Optimize db migration
  17. Restrict auto-generated routes.
  18. Remove tab
  19. Active Record Query Interface Optimization
  20. Create base controller
  21. Namespaced models
  22. Keep code struture in models consistent
  23. split route namespaces into different files
  24. Remove empty helpers
  25. Annotate your models
  26. defer expensive job
  27. comment your magic codes
  28. Simplify render in controllers
  29. Simplify render in views
  30. remove trailing whitespace
  31. Use memoization
  32. Select specific fields for performance
  33. Use query attribute
  34. Generate polymorphic url
  35. Use batched finder for large data query
  36. DRY bundler in capistrano
  37. Put scripts at the bottom
  38. use OpenStruct when advance search
  39. Fetch current user in models
  40. Use say and say_with_time in migrations to make a useful migration log
  41. Use STI and polymorphic model for multiple uploads
  42. to_s/to_s(:short)
  43. Use css sprite automatically
  44. Use I18n.localize for date/time formating
  45. Use multipart/alternative as content_type of email
  46. Use asset_host for production server
  47. Use render :collection Rails when possible
  48. DRY your database.yml
  49. Use nested step to improve readability of ur scenario
  50. Use ‘Background’ to consolidate common steps in a feature
  51. Writing specs for 3rd party declaratives
  52. Write ur own spec macros
  53. Double-check your migrations
  54. Use caching !
  55. Fix N+1 Queries
  56. Use Observer
  57. Use before_filter
  58. the Law of Demeter
  59. Replace instance variable with local variable
  60. Move code into model
  61. Move code into helper
  62. Move code into controller
  63. Isolating Seed Data
  64. Extract to composed class
  65. Extract into Module
  66. DRY Metaprogramming
  67. DRY Controller (debate)
  68. Always add DB index
  69. Love named_scope
  70. Keep Finders on Their Own Model
  71. Overuse route customizations
  72. Not use default route if you use RESTful design
  73. Nested Model Forms
  74. Needless deep nesting
  75. model.collection_model_ids (many-to-many)
  76. Use model callback
  77. Replace Complex Creation with Factory Method
  78. Move Model Logic into the Model
  79. Add model virtual attribute
  80. Use scope access
  81. Use model association
  82. Move finder to named_scope