HasMany relationships load all objects and then count on collection page
Created by: RCulpepp
- What were you trying to do?
I was attempting to load an index of resources that had hundreds of thousands of associated resources through a
has_many
relationship. When querying, it is attempting to load all objects in memory, before counting. Thus slowing down the app (unnecessarily and unexpectedly) since all that displays is a count for those associated objects.
I have something like this:
resource_dashboard.rb
ATTRIBUTE_TYPES = {
associated_resources: Field::HasMany
}.freeze
COLLECTION_ATTRIBUTES = [
:associated_resources,
].freeze
- What did you end up with (logs, or, even better, example apps are great!)? A very slow response!
I've come up with a solution that'll I'll submit as a PR.
- What versions are you running?
- Rails 5.0.7.2
- administrate 0.11.0