What You Haven’t Seen
This implementation of categories is like a filesystem. Each directory has a set of files,
just as each of our categories has a set of photos. Another macro, called
acts_as_nested_set, would be useful if you wanted to find all of the folders
in the All tree. Active Record is too big to cover in
detail in such a short book, but we’ll point you to a few things that you
should know:
- Event callbacks
Active Record can call a method when an event happens, such as before a save, after creation, or when a model changes.
- Reflection
Active Record will let you find attributes (as you’ve seen), and will also find all of the associations, or relationships, defined on a model.
- Versioning
Active Record uses the column
lock_version, if it exists, to manage concurrency using a technique called optimistic locking. With this technique, a database engine can store multiple versions of each piece of data and maintain database integrity if many applications need the same piece of data.- Count caching
Rather than using SQL to compute the number of certain types of objects, Active Record can cache the number of items in a collection for performance.
- Polymorphic associations
You can design a model that has
belongs_torelationships to more than one model. For example, anaddresscan belong to apersonand ashipment.- Enhancements
Active Record gets enhanced often. We recommend that you periodically check the documentation and watch the various Rails mailing lists if you’re going to be doing regular ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access