Module: Goldiloader::ThroughAssociationPatch

Defined in:
lib/goldiloader/active_record_patches.rb

Instance Method Summary collapse

Instance Method Details

#auto_include?Boolean

Returns:

  • (Boolean)


177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/goldiloader/active_record_patches.rb', line 177

def auto_include?
  # Only auto include through associations if the target association is auto-loadable
  through_association = owner.association(through_reflection.name)
  auto_include_self = super

  # If the current association cannot be auto-included there is nothing we can do
  return false unless auto_include_self

  # If the through association can just be auto-included we're good
  return true if through_association.auto_include?

  # If the through association was already loaded and does not contain new, changed, or destroyed records
  # we are also able to auto-include the association. It means it has only already been read or changes are
  # already persisted.
  through_association.loaded? && Array.wrap(through_association.target).none? do |record|
    record.new_record? || record.changed? || record.destroyed?
  end
end