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
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
beforeafter 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.
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:
pluckpulls 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
- 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
afterhooks instead of in a particular spec to ensure their purging.