Created
March 25, 2016 18:19
-
-
Save brianjlandau/ebb144a5df1bee237a3d to your computer and use it in GitHub Desktop.
This can be used so that when building records via nested attributes the correct subclass is used when creating the record.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ActiveRecord::Reflection::AbstractReflection.class_eval do | |
def build_association(*options, &block) | |
attributes = if options.first.is_a?(Hash) | |
options.first.with_indifferent_access | |
end | |
if attributes && attributes[:type].present? | |
attributes[:type].constantize.new(*options, &block) | |
else | |
klass.new(*options, &block) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment