Rails supports six types of associations:
Sets up a one-to-one connection with another model,
Each instance of the declaring model "belongs to" one instance of the other model.
For example, if your application includes customers and orders, and each order can be assigned to exactly one customer
sets up a one-to-one connection with another model
each instance of a model contains or possesses one instance of another model.
Indicates a one-to-many connection with another model.
Often found on the "other side" of a
each instance of the model has zero or more instances of another model.
For example, in an application containing customers and orders, the customer model could be declared like this:
Used to set up a many-to-many connection with another model.
The declaring model can be matched with zero or more instances of another model by proceeding through a third model.
For example, consider a medical practice where patients make appointments to see physicians. The relevant association declarations could look like this:
The collection of join models can be managed via the API. For example, if you assign
new join models are created for newly associated objects, and if some are gone their rows are deleted.
"shortcuts" through nested has_many associations.
Automatic deletion of join models is direct, no destroy callbacks are triggered.
For example, if a document has many sections, and a section has many paragraphs, you may sometimes want to get a simple collection of all paragraphs in the document. You could set that up this way:
class Document < ActiveRecord::Base
has_many :paragraphs, through: :sections
class Section < ActiveRecord::Base
class Paragraph < ActiveRecord::Base
With through: :sections specified, Rails will now understand:
sets up a one-to-one connection with another model.
The declaring model can be matched with one instance of another model by proceeding through a third model.
For example, if each supplier has one account, and each account is associated with one account history, then the supplier model could look like this:
creates a direct many-to-many connection with another model, with no intervening model.
For example, if your application includes assemblies and parts, with each assembly having many parts and each part appearing in many assemblies, you could declare the models this way:
Artificial Intelligence + NLP + deep learning > Tools - Business > Ruby > cs169 > 3- Advanced Rails >