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