Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianjlandau/ebb144a5df1bee237a3d to your computer and use it in GitHub Desktop.
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.
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