Project

General

Profile

Actions

Refactor #16172

closed

Remove base_class override in template subclasses

Added by Dominic Cleal over 8 years ago. Updated over 6 years ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
Rails
Target version:
Difficulty:
Triaged:
Fixed in Releases:
Found in Releases:

Description

Both ProvisioningTemplate and Ptable contain overrides of the Rails base_class method, indicating that the STI base class of ProvisioningTemplate (etc) is ProvisioningTemplate, rather than Template.

# we have to override the base_class because polymorphic associations does not detect it correctly, more details at
# http://apidock.com/rails/ActiveRecord/Associations/ClassMethods/has_many#1010-Polymorphic-has-many-within-inherited-class-gotcha
def self.base_class
  self
end
# not sure why but this changes table_name so we set it explicitly
self.table_name = 'templates'

This causes an issue with the Template model instantiation under Rails 5.0, as ActiveRecord performs casting of new models, much like Foreman::STI does. This relies on base_class returning the actual base class, else the inheritance appears inverted and causes this exception:

DomainTest#test_0008_should update total_hosts on changing primary interface domain:
ActiveRecord::SubclassNotFound: Invalid single-table inheritance type: Template is not a subclass of Ptable
    activerecord (5.0.0) lib/active_record/inheritance.rb:189:in `find_sti_class'
    activerecord (5.0.0) lib/active_record/inheritance.rb:209:in `subclass_from_attributes'
    activerecord (5.0.0) lib/active_record/inheritance.rb:58:in `new'

Removing base_class causes the problem noted in the comment above, that polymorphic associations to these models break - in fact, they search for the base class rather than the specific class. https://github.com/rails/rails/blob/v5.0.0/activerecord/lib/active_record/associations.rb#L780 notes that in actual fact, the base class should be stored in the type column for polymorphic associations, not the derivative.

This should be changed to remove the overrides and store the base class when dealing with polymorphic STI associations (both Taxonomy and Audit are associated to Template derivatives).


Related issues 1 (1 open0 closed)

Related to Foreman Remote Execution - Refactor #16195: Remove base_class override in job_templateNew08/19/2016Actions
Actions #1

Updated by The Foreman Bot over 8 years ago

  • Status changed from Assigned to Ready For Testing
  • Pull request https://github.com/theforeman/foreman/pull/3745 added
Actions #2

Updated by Marek Hulán over 8 years ago

  • Related to Refactor #16195: Remove base_class override in job_template added
Actions #3

Updated by Marek Hulán over 8 years ago

  • Translation missing: en.field_release set to 160
Actions #4

Updated by Dominic Cleal over 8 years ago

  • Status changed from Ready For Testing to Closed
  • % Done changed from 0 to 100
Actions

Also available in: Atom PDF