Class: DynamicActiveModel::HasManyThrough

Inherits:
Object
  • Object
show all
Defined in:
lib/dynamic-active-model/has_many_through.rb

Overview

Adds has_many :through associations across join models. A join model is a table with a primary key and a unique index on exactly two columns, each a belongs_to to a different model; other columns (timestamps, audit columns, extra data) don't matter. For user_roles with a unique (user_id, role_id):

User has_many :roles, through: :user_roles, source: :role
Role has_many :users, through: :user_roles, source: :user

A name already taken on the model (association, method or column) is skipped. Tables without a primary key are has_and_belongs_to_many join tables instead.

Instance Method Summary collapse

Constructor Details

#initialize(models, table_indexes) ⇒ HasManyThrough

Returns a new instance of HasManyThrough.

Parameters:

  • models (Array<Class>) —

    Models with their belongs_to/has_many already built

  • table_indexes (Hash) —

    Indexes by table name (see Associations#table_indexes)



17
18
19
20
# File 'lib/dynamic-active-model/has_many_through.rb', line 17

def initialize(models, table_indexes)
  @models = models
  @table_indexes = table_indexes
end

Instance Method Details

#build! ⇒ void

This method returns an undefined value.



23
24
25
# File 'lib/dynamic-active-model/has_many_through.rb', line 23

def build!
  @models.each { |model| add_through_associations(model) }
end