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
andbefore
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"]
forrender
ing instead offlash["alert message"]
. The latter carries over to the next request which is why it is used withredirect_to
- a new request.
Other tips i appreciated in my code review were:
- Use
pluck
instead ofmap
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 usingmap
. However, here’s an article that talks about whenpluck
is unnecessary. - For efficient single-record search, use
find_by(attribute: value)
than the fancyfind_by_attribute(value)
, which goes throughmethod_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
inbefore
orafter
hooks instead of in a particular spec to ensure their purging.