I''m experiencing this exact behavior using STI -------------------------------------------------------------------------------------------- http://www.techlists.org/archives/programming/railslist/2005-12/msg00202.shtml Here''s a concrete example. Say I have three classes that inherit from ActiveRecord, and I have one instance of each in my ''animals'' table: Dog < Mammal < Animal < ActiveRecord::Base Run these command in order and you get: Mammal.count() = 1 Dog.count() = 1 Mammal.count() = 2! Here''s the SQL: SHOW FIELDS FROM animals SELECT COUNT(*) FROM animals WHERE ( (animals.`type` = ''Mammal'' ) ) SHOW FIELDS FROM animals SELECT COUNT(*) FROM animals WHERE ( (animals.`type` = ''Dog'' ) ) SELECT COUNT(*) FROM animals WHERE ( (animals.`type` = ''Mammal'' OR animals.`type` = ''Dog'' ) ) -------------------------------------------------------------------------------------------- I noticed what''s making this happen is the ActiveRecord::Base @@subclasses class variable handled by this attribute. def subclasses @@subclasses[self] ||= [] @@subclasses[self] + extra @@subclasses[self].inject([]) {|list, subclass| list | subclass.subclasses } end Is there a way to "eager load" this variable with @@subclasses[Mammel] = [Dog] so that whenever I do Mammel.find(:all), I''m also retrieving records with Dog? I could switch to polymorphic and it would probably make my application more scalable, but I''d prefer to stay on one table to maintain simplicity. This is madness and my head hurts. Anyone have any suggestions? Thanks Kevin --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Are the subclasses in the same file as the base class? If not, ''require'' or ''load'' might do the trick. On Mar 21, 10:09 am, Kevin Wu <friskyg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m experiencing this exact behavior using STI > --------------------------------------------------------------------------------------------http://www.techlists.org/archives/programming/railslist/2005-12/msg00... > Here''s a concrete example. Say I have three classes that inherit > from > ActiveRecord, and I have one instance of each in my ''animals'' table: > Dog < Mammal < Animal < ActiveRecord::Base > > Run these command in order and you get: > Mammal.count() = 1 > Dog.count() = 1 > Mammal.count() = 2! > > Here''s the SQL: > SHOW FIELDS FROM animals > SELECT COUNT(*) FROM animals WHERE ( (animals.`type` = ''Mammal'' ) ) > SHOW FIELDS FROM animals > SELECT COUNT(*) FROM animals WHERE ( (animals.`type` = ''Dog'' ) ) > SELECT COUNT(*) FROM animals WHERE ( (animals.`type` = ''Mammal'' OR > animals.`type` = ''Dog'' ) ) > -------------------------------------------------------------------------------------------- > > I noticed what''s making this happen is the ActiveRecord::Base > @@subclasses class variable handled by this attribute. > def subclasses > @@subclasses[self] ||= [] > @@subclasses[self] + extra > @@subclasses[self].inject([]) {|list, subclass| list | > subclass.subclasses } > end > > Is there a way to "eager load" this variable with @@subclasses[Mammel] > = [Dog] so that whenever I do Mammel.find(:all), I''m also retrieving > records with Dog? > > I could switch to polymorphic and it would probably make my > application more scalable, but I''d prefer to stay on one table to > maintain simplicity. This is madness and my head hurts. Anyone have > any suggestions? > > Thanks > > Kevin--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Dude man! Thanks SOOOOO MUCH. I added require_dependency ''model'' see the bottom of http://wiki.rubyonrails.org/rails/pages/SingleTableInheritance - DOH! if only I read that more closely. On Mar 21, 7:47 am, AndyV <a...-HmMyXyqgL2CVc3sceRu5cw@public.gmane.org> wrote:> Are the subclasses in the same file as the base class? If not, > ''require'' or ''load'' might do the trick. > > On Mar 21, 10:09 am, Kevin Wu <friskyg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I''m experiencing this exact behavior using STI > > --------------------------------------------------------------------------------------------http://www.techlists.org/archives/programming/railslist/2005-12/msg00... > > Here''s a concrete example. Say I have three classes that inherit > > from > > ActiveRecord, and I have one instance of each in my ''animals'' table: > > Dog < Mammal < Animal < ActiveRecord::Base > > > Run these command in order and you get: > > Mammal.count() = 1 > > Dog.count() = 1 > > Mammal.count() = 2! > > > Here''s the SQL: > > SHOW FIELDS FROM animals > > SELECT COUNT(*) FROM animals WHERE ( (animals.`type` = ''Mammal'' ) ) > > SHOW FIELDS FROM animals > > SELECT COUNT(*) FROM animals WHERE ( (animals.`type` = ''Dog'' ) ) > > SELECT COUNT(*) FROM animals WHERE ( (animals.`type` = ''Mammal'' OR > > animals.`type` = ''Dog'' ) ) > > -------------------------------------------------------------------------------------------- > > > I noticed what''s making this happen is the ActiveRecord::Base > > @@subclasses class variable handled by this attribute. > > def subclasses > > @@subclasses[self] ||= [] > > @@subclasses[self] + extra > > @@subclasses[self].inject([]) {|list, subclass| list | > > subclass.subclasses } > > end > > > Is there a way to "eager load" this variable with @@subclasses[Mammel] > > = [Dog] so that whenever I do Mammel.find(:all), I''m also retrieving > > records with Dog? > > > I could switch to polymorphic and it would probably make my > > application more scalable, but I''d prefer to stay on one table to > > maintain simplicity. This is madness and my head hurts. Anyone have > > any suggestions? > > > Thanks > > > Kevin--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---