STI find(:id) bug

Hi, on Rails 2.2RC1 I've found a what appears to be a bug in ActiveRecord STI classes:

class ItemCollection < ActiveRecord::Base class Order < ItemCollection class ReplenishmentOrder < Order

The problem is that instances of ReplenishmentOrder are initially not found via Order.find(:id), as shown below. After searching the subclass directly (second query), the SQL of searching the parent class changes (see third query in log below).

Console:

Order.find(510)

ActiveRecord::RecordNotFound: Couldn't find Order with ID=510         from /Users/zubin/ww/g2/vendor/rails/activerecord/lib/ active_record/base.rb:1528:in `find_one'         from /Users/zubin/ww/g2/vendor/rails/activerecord/lib/ active_record/base.rb:1511:in `find_from_ids'         from /Users/zubin/ww/g2/vendor/rails/activerecord/lib/ active_record/base.rb:590:in `find'         from (irb):1

ReplenishmentOrder.find(510)

=> #<ReplenishmentOrder id: 510, parent_id: nil, user_id: 28, payment_method_id: nil, billing_address_id: 1, shipping_address_id: 1, type: "ReplenishmentOrder", status: "editing", ref: nil, despatched_at: nil, created_at: "2008-10-28 04:42:54", updated_at: "2008-10-29 05:04:30", total: #<BigDecimal:5d92998,'0.0',4(8)>, freight_total: #<BigDecimal:5d9295c,'0.0',4(8)>, redeemed_total: #<BigDecimal:5d92920,'0.0',4(8)>>

Order.find(510)

=> #<ReplenishmentOrder id: 510, parent_id: nil, user_id: 28, payment_method_id: nil, billing_address_id: 1, shipping_address_id: 1, type: "ReplenishmentOrder", status: "editing", ref: nil, despatched_at: nil, created_at: "2008-10-28 04:42:54", updated_at: "2008-10-29 05:04:30", total: #<BigDecimal:5d8ff54,'0.0',4(8)>, freight_total: #<BigDecimal:5d8ff18,'0.0',4(8)>, redeemed_total: #<BigDecimal:5d8fedc,'0.0',4(8)>>

Log:

$ tail log/development.log   Order Columns (185.4ms) SHOW FIELDS FROM `item_collections`   Order Load (97.8ms) SELECT * FROM `item_collections` WHERE (`item_collections`.`id` = 510) AND ( (`item_collections`.`type` = 'Order' ) )   ReplenishmentOrder Columns (2.5ms) SHOW FIELDS FROM `item_collections`   ReplenishmentOrder Load (0.4ms) SELECT * FROM `item_collections` WHERE (`item_collections`.`id` = 510) AND ( (`item_collections`.`type` = 'ReplenishmentOrder' ) )   Order Load (0.5ms) SELECT * FROM `item_collections` WHERE (`item_collections`.`id` = 510) AND ( (`item_collections`.`type` = 'Order' OR `item_collections`.`type` = 'ReplenishmentOrder' ) )

-Zubin

Hi, on Rails 2.2RC1 I've found a what appears to be a bug in ActiveRecord STI classes:

This isn't new. It's in fact enough to just load the
ReplenishmentOrder class (ie require_dependency 'replenishment_order')
for Order.find to find instances of ReplenishmentOrder. Until that has
happened the ReplenishmentOrder class doesn't even exist and so
activerecord doesn't know to add it to the type condition. This is a
little fiddly.

Fred

This probably happens because you're in development mode where the AR model classes are not all automatically loaded, so if you find through Order you only get Order and ItemCollection because neither is aware of the existance of ReplenishmentOrder until it has been loaded.

A hack fix for this is to put: "require_association 'order'" at the bottom of item_collection.rb and "require_association 'replenishment_order'" at the bottom of order.rb

Thanks guys, that did the trick. -Zubin