Today we learned about ActiveRecord Callbacks. Callbacks are methods that are called at different stages of an object’s ‘life’ or cycle. We implement callbacks in our models whenever a change to one object affects data in an associated object. (There are other scenarios too, like when you want to modify data before it’s saved.)
A real life example would be a company and employees. If the company closes down (company object changes), then all the employees are not employees anymore! This would be an example where we would use before_destroy. Before deleting the company object (and associated company row in the database), you would delete all the employees too.
There’s an even better way to do this –> has_many :employees, dependent: :destroy. Destroy makes the method sound so epic.
We also had the privilege of having Stephen Steneker from MongoDB come in and give a talk on MongoDB database schema design. A lot of it unfortunately went over my head, but it was interesting to see a different approach to storing data (bson versus the tables in relational databases).
Tomorrow is Assessment #2 on ActiveRecord and SQL… it has been a crazy week in that we just started AR late Tuesday and have a test on Friday morning! I feel like I’ve done most of the assignments but am not sure if I have learned it… SQL also feels like weeks ago even though it was last weekend.
Most important skill at Lighthouse Labs: learn to learn really fast.