Learning Ruby

Reference, Mnemonic & Ramblings

Takeaways From Course 3 - Part 3

These are some key takeaways from the 3rd course for me based on the curriculum as well as the feedback received from the TAs for every module (aka Week).

In Module 4, focus was on learning…
  • self-referential associations (a user following another user)
  • ActionMailer: sending emails
  • token generation for password reseting

Module 4

This week i was reminded of some basics again (which i had forgotten!). Primarily:
  • Avoid rendering from a POST request unless there’s an error.
  • Try to keep the test setup close to the action and assertion (double check the use of let and before after writing specs). This helps in readability of the specs.
  • Single-Assertion Principle (more of a guideline than a law): Avoid unrelated assertions in a single spec.
  • Name the shared_examples well enough to be understood clearly.
  • Use flash.now["alert message"] for rendering instead of flash["alert message"]. The latter carries over to the next request which is why it is used with redirect_to - a new request.
Other tips i appreciated in my code review were:
  • Use pluck instead of map for performance. pluck pulls only the ids from the database and loads the data into an array. It saves a lot of processing time and power over loading every record into memory using map. However, here’s an article that talks about when pluck is unnecessary.
  • For efficient single-record search, use find_by(attribute: value) than the fancy find_by_attribute(value), which goes through method_missing, and doesn’t even save much typing!
  • Avoid writing migrations for populating data. Write a custom rake task under ‘lib/tasks’ instead.
  • Clear ActionMailer deliveries using ActionMailer::Base.deliveries.clear in before or after hooks instead of in a particular spec to ensure their purging.

comments powered by Disqus