ActiveRecord builds instance of wrong class through a scope targeting an STI class
问题 I would like to be able to call the build method on a scope that targets a certain class of model via its STI type, and have ActiveRecord build an instance of the correct class. class LineItem < ActiveRecord::Base scope :discount, where(type: 'DiscountLineItem') end class DiscountLineItem < LineItem; end > LineItem.discount.build # Expect an instance of DiscountLineItem here => #<LineItem ...> Here, I expected an instance of DiscountLineItem , not an instance of LineItem . 回答1: Even though