railsadminteam / rails_admin

RailsAdmin is a Rails engine that provides an easy-to-use interface for managing your data
MIT License
7.87k stars 2.25k forks source link

Associated records not shown when associations have a default scope #3674

Open rnevius opened 4 months ago

rnevius commented 4 months ago

Preface

This could be my own misunderstanding about how scoping associated models is expected to configured. I have reviewed the Wiki.

Describe the bug

Let's say we have 2 models, Team and Player. Player belongs_to :team and Team has_many :players. The Player model has a default_scope which excludes "inactive" players:

class Team < ApplicationRecord
  has_many :players, dependent: :destroy

  default_scope { where(active: true) }

  rails_admin do
    configure :players do
      associated_collection_cache_all false
      associated_collection_scope do
        proc { |scope| scope.unscoped }
      end
    end
  end
end

class Player < ApplicationRecord
  belongs_to :team

  default_scope { where(active: true) }

  rails_admin do
    scope { Player.unscoped }
  end
end

Players are unscoped and shown in the /player route in Rails Admin, as expected:

image

🐛 However, Players are not shown when viewing Teams (list, edit, show, etc.):

image

Reproduction steps

Demo repo: https://github.com/rnevius/rails_admin_associated_scope_demo

Relevant code is in the models.

Expected behavior

Unscoped associations should be shown.

Additional context