[26691] Avoid find(ids) in eager loader

The `WorkPackageCollectionEagerLoading` uses eager loading for eager loading CF values
that MAY not exist anymore. This results in a faulty query when hitting one of these entries.

```
E, [2017-11-29T17:25:21.847832 #14241] ERROR -- :   Grape rescuing from error: API::Errors::NotFound

  Original error: #<ActiveRecord::RecordNotFound: Couldn't find Version with 'id'=35>

  Stacktrace:

    /opt/openproject/vendor/bundle/ruby/2.4.0/gems/activerecord-5.0.4/lib/active_record/relation/finder_methods.rb:353:in `raise_record_not_found_exception!'
    /opt/openproject/vendor/bundle/ruby/2.4.0/gems/activerecord-5.0.4/lib/active_record/relation/finder_methods.rb:479:in `find_one'
    /opt/openproject/vendor/bundle/ruby/2.4.0/gems/activerecord-5.0.4/lib/active_record/relation/finder_methods.rb:458:in `find_with_ids'
    /opt/openproject/vendor/bundle/ruby/2.4.0/gems/activerecord-5.0.4/lib/active_record/relation/finder_methods.rb:66:in `find'
    /opt/openproject/vendor/bundle/ruby/2.4.0/gems/activerecord-5.0.4/lib/active_record/querying.rb:3:in `find'
    /opt/openproject/vendor/bundle/ruby/2.4.0/gems/activerecord-5.0.4/lib/active_record/core.rb:151:in `find'
    /opt/openproject/lib/api/v3/work_packages/work_package_collection_representer.rb:228:in `eager_load_custom_values'
    /opt/openproject/lib/api/v3/work_packages/work_package_collection_representer.rb:216:in `eager_load_version_custom_values'
    /opt/openproject/lib/api/v3/work_packages/work_package_collection_representer.rb:197:in `full_work_packages'
    /opt/openproject/lib/api/v3/work_packages/work_package_collection_representer.rb:72:in `initialize'
    /opt/openproject/app/services/api/v3/work_package_collection_from_query_service.rb:125:in `new'
    /opt/openproject/app/services/api/v3/work_package_collection_from_query_service.rb:125:in `collection_representer'
    /opt/openproject/app/services/api/v3/work_package_collection_from_query_service.rb:57:in `results_to_representer'
    /opt/openproject/app/services/api/v3/work_package_collection_from_query_service.rb:46:in `call'
    /opt/openproject/lib/api/v3/queries/helpers/query_representer_response.rb:37:in `query_representer_response'
    /opt/openproject/lib/api/v3/queries/queries_api.rb:91:in `block (3 levels) in <class:QueriesAPI>'
```

https://community.openproject.com/wp/26691
pull/6027/head
Oliver Günther 7 years ago
parent 189eb6ace5
commit 376fa46d4b
  1. 2
      lib/api/v3/work_packages/work_package_collection_eager_loading.rb

@ -76,7 +76,7 @@ module API
ids_of_values = cvs.map(&:value).select { |v| v =~ /\A\d+\z/ }
values_by_id = scope.find(ids_of_values).group_by(&:id)
values_by_id = scope.where(id: ids_of_values).group_by(&:id)
cvs.each do |cv|
next unless values_by_id[cv.value.to_i]

Loading…
Cancel
Save