Module: ActiveRecord::WithEagerGroup

Defined in:
lib/active_record/with_eager_group.rb

Instance Method Summary collapse

Instance Method Details

#eager_group(*args) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/active_record/with_eager_group.rb', line 11

def eager_group(*args)
  # we does not use the `check_if_method_has_arguments!` here because it would flatten all the arguments,
  # which would cause `[:eager_group_definition, scope_arg1, scope_arg2]` not able to preload together with other `eager_group_definitions`.
  # e.g. `Post.eager_group(:approved_comments_count, [:comments_average_rating_by_author, students[0], true])`
  check_argument_not_blank!(args)
  check_argument_valid!(args)

  spawn.eager_group!(*args)
end

#eager_group!(*args) ⇒ Object



21
22
23
24
# File 'lib/active_record/with_eager_group.rb', line 21

def eager_group!(*args)
  self.eager_group_values |= args
  self
end

#eager_group_valuesObject



26
27
28
# File 'lib/active_record/with_eager_group.rb', line 26

def eager_group_values
  @values[:eager_group] || []
end

#eager_group_values=(values) ⇒ Object

Raises:

  • (ImmutableRelation)


30
31
32
33
34
# File 'lib/active_record/with_eager_group.rb', line 30

def eager_group_values=(values)
  raise ImmutableRelation if @loaded

  @values[:eager_group] = values
end

#exec_queriesObject



5
6
7
8
9
# File 'lib/active_record/with_eager_group.rb', line 5

def exec_queries
  records = super
  EagerGroup::Preloader.new(klass, records, eager_group_values).run if eager_group_values.present?
  records
end