Hi, I have the following setup: class Item < ActiveRecord::Base belongs_to :item, :polymorphic => true end class AbstractItem < ActiveRecord::Base has_one :item, :as => :item def self.abstract_class?; self == AbstractItem; end end class Post < AbstractItem end Now, when I do a post = Post.find :first, :include => :item inside my controller, post.item will be nil. I can do post = Post.find :first and then post.item will work, but it uses a second query. But if I don''t use AbstractItem and use Post as follow: class Post < ActiveRecord::Base has_one :item, :as => :item end Then post = Post.find :first, :include => :item will work just fine, takes one query, nada problems. The reason I would like an abstract model is because there will be a lot of identical functionality within all the item types. So, what''s the reason eager loading doesn''t work with abstract models. How can I solve this and/or is there another/better way to solve this. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Have you considered using Single Table Inheritance (STI) to implement this? With STI you can persist your objects in only one table, and use the inheritance to keep your classes DRY. On May 25, 5:18 pm, Dax Huiberts <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi, > > I have the following setup: > > class Item < ActiveRecord::Base > belongs_to :item, :polymorphic => true > end > > class AbstractItem < ActiveRecord::Base > has_one :item, :as => :item > def self.abstract_class?; self == AbstractItem; end > end > > class Post < AbstractItem > end > > Now, when I do a > post = Post.find :first, :include => :item > inside my controller, post.item will be nil. > I can do > post = Post.find :first > and then post.item will work, but it uses a second query. > > But if I don''t use AbstractItem and use Post as follow: > > class Post < ActiveRecord::Base > has_one :item, :as => :item > end > > Then > post = Post.find :first, :include => :item > will work just fine, takes one query, nada problems. > > The reason I would like an abstract model is because there will be a lot > of identical functionality within all the item types. > > So, what''s the reason eager loading doesn''t work with abstract models. > How can I solve this and/or is there another/better way to solve this. > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Yes, but the sub types will be very different, STI won''t be a nice option here. I saw a different approach using a module and include that one in the models, haven''t tried it yet. But still, it''s just weird that it doesn''t work as expected, and I can''t see any reason why not. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---