我希望能夠通過其STI類型針對特定類型的模型調用build
方法,並讓ActiveRecord構建一個實例正確的課程。ActiveRecord通過瞄準STI類的作用域構建錯誤類的實例
class LineItem < ActiveRecord::Base
scope :discount, where(type: 'DiscountLineItem')
end
class DiscountLineItem < LineItem; end
> LineItem.discount.build # Expect an instance of DiscountLineItem here
=> #<LineItem ...>
在這裏,我預計DiscountLineItem
一個實例,而不是LineItem
一個實例。
也許現在是時候把這個討論放到Rails跟蹤器中了。我會寫一個失敗的測試用例,並在今天發佈。 – steveluscher 2012-07-09 17:26:52
我昨晚編了一個修復程序,並將它發佈到rails核心谷歌小組以獲得反饋。我將在今晚晚些時候發佈這裏作爲更新,當我有機會的時候。 – 2012-07-09 17:39:44
@steveluscher以下是您可能想要嘗試的ActiveRecord補丁:https://gist.github.com/5cad22a11f011052d8f6 – 2012-07-10 02:25:40