On 26 Dec 2008, at 10:59, Luma wrote:>
>
> Now I have an instance of PurchasedItem and would like to get its
> foreign_item:
>
> foreign_item = purchased_item.foreign_item
> (purchased_item.foreign_item_id == 100, and the related foreign_item
> has type == ''Retailer1Item'' or type ==
''Retailer2Item'')
>
> The sql-query should look like:
> "SELECT FROM order_items WHERE order_items.id = 100 AND
> (order_items.type = ''ForeignItem'' OR order_items.type
> ''Retailer1Item'' OR order_items.type =
''Retailer2Item'')"
>
> but this is executed:
>
> [4;36;1mForeignItem Columns (0.007000) [0m [0;1mSHOW FIELDS FROM
> `order_items` [0m
> [4;35;1mForeignItem Load (0.001000) [0m [0mSELECT * FROM
> `order_items` WHERE (`order_items`.`id` = 100) AND
> ( (`order_items`.`type` = ''ForeignItem'' ) ) [0m
>
> Thus, the foreign_item cannot be found. The same when I explicitly use
> ForeignItem.find
>
This is STI magic. At this point the classes Retailer1Item and
Retailer2Item item have not been loaded and Active Record is not aware
of their existence. if you require them explicitly with
require_dependency (eg at the bottom of foreign_item.rb) then it
should do the right thing.
Fred>
> What can I do?
>
> Luma
> >
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---