Rails 3 2 counter cache not updating
For example, consider a simple Rails application that includes a model for authors and a model for books. Without associations, the model declarations would look like this: With Active Record associations, we can streamline these - and other - operations by declaratively telling Rails that there is a connection between the two models.
Because they make common operations simpler and easier in your code.
For option, Active Record creates the name by using the lexical order of the class names.
So a join between author and book models will give the default join table name of "authors_books" because "a" outranks "b" in lexical ordering..
For example, you might have a picture model that belongs to either an employee model or a product model.
Here's how this could be declared: In designing a data model, you will sometimes find a model that should have a relation to itself.
Associations are extremely useful, but they are not magic.
For example, you may want to store all employees in a single database model, but be able to trace relationships such as between manager and subordinates.
This situation can be modeled with self-joining associations: You are not free to use just any name for your associations.
You are responsible for maintaining your database schema to match your associations.
In practice, this means two things, depending on what sort of associations you are creating.
However, Active Record will not automatically identify bi-directional associations that contain a scope or any of the following options: association creates a one-to-one match with another model.