avo-hq / avo

Build Ruby on Rails apps 10x faster
https://avohq.io
Other
1.51k stars 242 forks source link

Polymorphic Associations Causing N+1 Issue #3045

Open vbiletskii opened 2 months ago

vbiletskii commented 2 months ago

Describe the bug

I have polymorphic associations in place, and when I try to create a Comment in the User view, I encounter an n+1 issue even though I have included the dependent association.

Steps to Reproduce

Steps to reproduce the behavior:

  1. You can use this repo https://github.com/vbiletskii/polymorphic_issue to reproduce the issue
  2. Go to localhost:3000/avo/resources/users/1
  3. Click on Create new comment
  4. Open rails logs
  5. See n+1(It actually loads user and admin personal information even if I create a comment for User)

Expected behavior & Actual behavior

Expected behavior: It loads only one record with its title (name) to display in the field. Actual behavior: It loads all records from types: [User, Admin] with their titles, causing an n+1 issue.

Models and resource files

class User < ApplicationRecord
  has_one :personal_information, class_name: 'Users::PersonalInformation'
  has_many :comments, as: :commentable

  delegate :first_name, :last_name, to: :personal_information, allow_nil: true

  def name
    "#{first_name} #{last_name}"
  end
end
class Admin < ApplicationRecord
  has_one :personal_information, class_name: 'Admins::PersonalInformation'
  has_many :comments, as: :commentable

  delegate :first_name, :last_name, to: :personal_information, allow_nil: true

  def name
    "#{first_name} #{last_name}"
  end
end
class Comment < ApplicationRecord
  belongs_to :commentable, polymorphic: true
end
class Avo::Resources::User < Avo::BaseResource
  self.title = :name

  def fields
    field :id, as: :number
    field :comments, as: :has_many
  end
end
class Avo::Resources::Comment < Avo::BaseResource
  def fields
    field :id, as: :idlo
    field :commentable,
      as: :belongs_to,
      polymorphic_as: :commentable,
      disabled: true,
      types: [User, Admin],
      default: -> { record }
  end
end

System configuration

Avo version: 3.10.1 Rails version: 7.1.3 Ruby version: 3.2.2 License type:

Are you using Avo monkey patches, overriding views or view components?

Screenshots or screen recordings

https://github.com/user-attachments/assets/011718b1-bbdd-4e52-a95a-8ea71784711d

Additional context

Impact

Urgency

adrianthedev commented 2 days ago

Interesting edge case. Can you please try to update the attach_scope on the Comment resource?

field :commentable,
      as: :belongs_to,
      polymorphic_as: :commentable,
      disabled: true,
      types: [User, Admin],
      default: -> { record },
      attach_scope: -> { query.include(:personal_information) }

The code above is untested. It might need tweaking.

vbiletskii commented 2 days ago

Interesting edge case. Can you please try to update the attach_scope on the Comment resource?

field :commentable, as: :belongs_to, polymorphic_as: :commentable, disabled: true, types: [User, Admin], default: -> { record }, attach_scope: -> { query.include(:personal_information) } The code above is untested. It might need tweaking.

Thanks for the help, @adrianthedev! It helps somewhat, but it still loads all associations from types when I just want to create a comment for one of them. Additionally, if there's another model like Booking (without an association like has_many :personal_information), it will throw an error.

adrianthedev commented 2 days ago

Got it @vbiletskii. Please create a minimal reproduction repo with all the models and resources that we can troubleshoot and we'll gladly apply a solution.

Please use this guide on how to create one.

https://docs.avohq.io/3.0/technical-support.html#reproduction-repository

vbiletskii commented 2 days ago

@adrianthedev Here's the repository: https://github.com/vbiletskii/polymorphic-issue. Let me know if you need anything else from me - really appreciate all your help!