Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix calling reference on polymorphic relationships
For the record: I am not really proud or satisfied with the code added here in it's current state, but it fixes the problem it seems. Recently, the `lib/extensions/ar_merge_conditions.rb file was removed from the repo as it was deemed no longer to be needed, and we had converted everything over to using AREL. The main method that was removed was the following: def self.apply_legacy_finder_options(options) unknown_keys = options.keys - LEGACY_FINDER_METHODS.map(&:first) raise "Unsupported options #{unknown_keys}" unless unknown_keys.empty? # Determine whether any of the included associations are polymorphic has_polymorphic = included_associations(options[:include]).any? { |name| self.reflection_is_polymorphic?(name) } LEGACY_FINDER_METHODS.inject(all) do |scope, (key, method)| # Don't call references method on scope if polymorphic associations are # included to avoid ActiveRecord::EagerLoadPolymorphicError next(scope) if method == :references && has_polymorphic # options[key] ? scope.send(method, options[key]) : scope end end Which basically mapped the old Rails 2.3 methods (LEGACY_FINDER_METHODS, not shown) to the new(er) Rails 3+ AREL syntax. The important part of note is the lines with `has_polymorphic`, which prevents applying the values from the `:include` key (which is mapped to both the `.includes` and `.reference` methods) to the `.reference` method. When the refactoring was done in 062263c (part of the ManageIQ#10175 pull request), the check for the reference methods was not brought along, so when an invalid `:include_for_find` key was passed in with a polymorphic relationship, the code would break with a not so obvious error message: Error caught: [ActiveRecord::ConfigurationError] nil Most of the code in ar_merge_conditions.rb is actually for properly determining when a polymorphic relationship exists, so in reality, this code maybe should have stayed in some way or another. The changes here are an attempt to reduce the amount of code added, but it might be to limited in what keys are checked, and thus, not thorough enough. Regardless, it does seem to prevent most cases.
- Loading branch information