Skip to content

Loading…

Use given type as class when bulding STI associations #4130

Closed
grosser opened this Issue · 1 comment

2 participants

@grosser

e.g. when using nested attributes to build new associations they always have the wrong class without this patch

# Make build associations have their given type
# Makes that: user.persons.build(:type=>Manager).class == Manger
class ActiveRecord::Reflection::AssociationReflection
  def build_association(*options)
    if options.first.is_a?(Hash) and options.first[:type].presence
      options.first[:type].to_s.constantize.new(*options)
    else
      klass.new(*options)
    end
  end
end
@pixeltrix
Ruby on Rails member

Duplicate of #1686, #3749 and #3750.

@pixeltrix pixeltrix closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.