Rails includes with has_many through giving avoid eger loading n+1 by bullet gem

359 views Asked by At

My Models are like:

class ThreeDModel < ApplicationRecord
  has_many :three_d_model_animations
  has_many :animations, through: :three_d_model_animations
  has_many :three_d_model_images, dependent: :destroy
  has_many :three_d_garments
  has_one :model_efm
  has_one :model_edm
end

2nd:

    class ThreeDModelAnimation < ApplicationRecord
      belongs_to :animation
      belongs_to :three_d_model
      validates_presence_of :animation_file
      #validates_presence_of :motion
      mount_uploader :animation_file, ThreeDModelAnimationUploader
      enum animation_motions: {
          'Run': 'run',
          'Turn Aroun (100 Frames)': 'turn_around_100_frames',
          'Turn Around (300 Frames)': 'turn_around_300_frames',
          'Walk (58 Frames)': 'walk_58_frames',
          'Walk (92 Frames)': 'walk_92_frames'
      }
    end

3rd:

class Animation < ApplicationRecord
  has_many :three_d_model_animations
  has_many :three_d_models, through: :three_d_model_animations
  mount_uploader :video, AnimationVideoUploader
  validates_presence_of :video

  validates :name, :frames, :loop_start, :loop_end, presence: true
end

Now my query is:

    @model = ThreeDModel.where(id: params[:id]).includes(:model_efm, :model_edm, :three_d_model_images, :three_d_model_animations, :animations).last

which gives the following message by the bullet gem:

AVOID eager loading detected
  ThreeDModel => [:three_d_model_animations, :animations]

is there a better way so that n+1 could be avoided. Thanks in advance

0

There are 0 answers