Class: AmsLazyRelationships::Loaders::SimpleBelongsTo

Inherits:
Base
  • Object
show all
Defined in:
lib/ams_lazy_relationships/loaders/simple_belongs_to.rb

Overview

Batch loads parent ActiveRecord records for given record by foreign key. Useful when the relationship is not a standard ActiveRecord relationship.

Instance Method Summary collapse

Methods inherited from Base

#load

Constructor Details

#initialize(association_class_name, foreign_key: "#{association_class_name.underscore}_id") ⇒ SimpleBelongsTo

Returns a new instance of SimpleBelongsTo.

Parameters:

  • association_class_name (String)

    The name of AR class being the parent record of the records being loaded. E.g. When loading comment.blog_post it’d be “BlogPost”.

  • foreign_key (Symbol/String) (defaults to: "#{association_class_name.underscore}_id")

    Name of the foreign key column E.g. When loading comment.blog_post it’d be “blog_post_id



15
16
17
18
19
20
21
# File 'lib/ams_lazy_relationships/loaders/simple_belongs_to.rb', line 15

def initialize(
  association_class_name,
  foreign_key: "#{association_class_name.underscore}_id"
)
  @association_class_name = association_class_name
  @foreign_key = foreign_key.to_sym
end