ActiveRecord associations and more Part 1

  • Represent models and their data.
  • Represent associations between these models.
  • Represent inheritance hierarchies through related models.
  • Validate models before they get persisted to the database.
  • Perform database operations in an object-oriented fashion.
  • belongs_to
  • has_one
  • has_many
  • has_many :through
  • has_one :through
  • has_and_belongs_to_many

belongs_to

A belongs_to association sets up a 1-to-1 relation with another model, meaning that each instance of the declaring model belongs to one instance of the other model. For instance, if your business model includes Albums and Artists, and each Album can be assigned to exactly one Artist

  • association
  • association=(associate)
  • build_association(attributes = {})
  • create_association(attributes = {})
  • create_association!(attributes = {})
  • reload_association

has_one

has_one association is setup similar to belongs_to but unlike belongs_to association, has_one association tells us that every instance of a model contains only one instance of other model.

  • association
  • association=(associate)
  • build_association(attributes = {})
  • create_association(attributes = {})
  • create_association!(attributes = {})
  • reload_association

has_many

A has_many association tells us that the model has a one-to-many connection with another model. This association is common to see on the opposite side of a belongs_to association. This association indicates that each instance of the model has zero or more instances of another model. For instance, in a business model containing Artists and Albums, the Artist model could be declared like following;

  • collection
  • collection<<(object, ...)
  • collection.delete(object, ...)
  • collection.destroy(object, ...)
  • collection=(objects)
  • collection_singular_ids
  • collection_singular_ids=(ids)
  • collection.clear
  • collection.empty?
  • collection.size
  • collection.find(...)
  • collection.where(...)
  • collection.exists?(...)
  • collection.build(attributes = {}, ...)
  • collection.create(attributes = {})
  • collection.create!(attributes = {})
  • collection.reload

has_many :through

Now we are getting into very useful and powerful associations. A has_many :through association sets up a many-to-many(n-n) connection with another model, indicating that the declaring model can be matched with multiple instances of another model through a third model. For instance, in a album collection web app where users owns albums and have reviews on them, association declarations would look like this:

has_one :through

Very close to has_many :through but as the name suggests this kind of association sets up one-to-one relationship with another model;

has_and_belongs_to_many

A has_and_belongs_to_many association creates a direct many-to-many connection with another model. In this case, no third or intervening model is needed like in has_many :through association.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ali Erbay

Ali Erbay

7 Followers

Full Stack Developer BDD/TDD || Ruby on Rails || React || React Native || NodeJS || Express https://github.com/kermit-klein