Ruby on Rails ActiveRecord Associations The has_and_belongs_to_many association


Example

A has_and_belongs_to_many association 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:

class Assembly < ApplicationRecord
  has_and_belongs_to_many :parts
end

class Part < ApplicationRecord
  has_and_belongs_to_many :assemblies
end