tag:blogger.com,1999:blog-8917931115554225973.post7739353535139143867..comments2023-10-29T03:58:45.992-07:00Comments on Sleepless Coding: Enumerate all Rails ActiveRecord::Base model classesscottwbhttp://www.blogger.com/profile/07534230274246537772noreply@blogger.comBlogger2125tag:blogger.com,1999:blog-8917931115554225973.post-49046534571306814242010-08-24T23:12:19.474-07:002010-08-24T23:12:19.474-07:00Nice! Funny, a `subclasses` method was the first t...Nice! Funny, a `subclasses` method was the first thing I tried out of the blue...but I tried it in straight irb, not script/console. Object.subclasses must be added by Rails.<br /><br />Your solution actually DOES include all the STI classes (and also includes one other subclass in my project that is not mapped to a table), which is probably more "correct".<br /><br />I'm not sure how the lazy loading would play out for my particular scenario, but on this surface, this way seems like a winner.scottwbhttps://www.blogger.com/profile/07534230274246537772noreply@blogger.comtag:blogger.com,1999:blog-8917931115554225973.post-51549805156842826512010-08-24T21:38:03.208-07:002010-08-24T21:38:03.208-07:00Mark and I were looking over this and it occurred ...Mark and I were looking over this and it occurred to me that Rails already gives you the ability to do this. AR::Base has a protected method `subclasses`.<br /><br />In a script/console this won't give you all classes if you haven't loaded them into memory (thanks to lazy autoloading), but `ActiveRecord::Base.send :subclasses` will return an array of all models.<br /><br />The only thing that might differ from yours is that I am not sure if it will return STI classes (i.e. second-level subclasses)Anonymousnoreply@blogger.com