Use given type as class when bulding STI associations #4130

Closed
grosser opened this Issue Dec 22, 2011 · 1 comment

Comments

Projects
None yet
2 participants
@grosser
Contributor

grosser commented Dec 22, 2011

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

This comment has been minimized.

Show comment
Hide comment
@pixeltrix

pixeltrix Dec 22, 2011

Member

Duplicate of #1686, #3749 and #3750.

Member

pixeltrix commented Dec 22, 2011

Duplicate of #1686, #3749 and #3750.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment