Matthew Beale
2006-Apr-03 16:57 UTC
[Rails] Is old school STI broken? How would you do the same now?
So this new STI :through thing is still not clear to me. I have a project now where I''m using this: class List < ActiveRecord::Base belongs_to :user end class StaticList < List has_and_belongs_to_many :items end In the script/console: >> StaticList.create NameError: uninitialized constant StaticList What? ok, fine. So a List.create would be in order, except no matter what is passed into :type =>, it ends up with "List" in the database''s type column. Is there a better way to model this with the :through feature? Why isn''t rails finding the StaticList class? Is this a bug? A feature? The definition is in the same file as class List... Thanks for any pointers, -- Matthew Beale :: mixonic@synitech.com Resume & Portfolio @ http://madhatted.com -- Posted via http://www.ruby-forum.com/.
Chris T
2006-Apr-03 17:28 UTC
[Rails] Is old school STI broken? How would you do the same now?
Have you declared the list model in application.rb? class ApplicationController < ActionController::Base model :list You need to do this so the application knows about StaticList class (It will only know about the subclass once it''s read the list.rb model declaration) Hope this helps. Chris T Matthew Beale wrote:> So this new STI :through thing is still not clear to me. I have a > project now where I''m using this: > > class List < ActiveRecord::Base > belongs_to :user > end > > class StaticList < List > has_and_belongs_to_many :items > end > > In the script/console: > > >> StaticList.create > NameError: uninitialized constant StaticList > > What? ok, fine. So a List.create would be in order, except no matter > what is passed into :type =>, it ends up with "List" in the database''s > type column. Is there a better way to model this with the :through > feature? Why isn''t rails finding the StaticList class? Is this a bug? A > feature? The definition is in the same file as class List... > > Thanks for any pointers, > > -- > Matthew Beale :: mixonic@synitech.com > Resume & Portfolio @ http://madhatted.com > >
twifkak@comcast.net
2006-Apr-03 17:55 UTC
[Rails] Is old school STI broken? How would you do the same now?
-------------- Original message ---------------------- From: Chris T <ctmailinglists@googlemail.com>> Have you declared the list model in application.rb? > > class ApplicationController < ActionController::Base > model :list > > You need to do this so the application knows about StaticList class (It > will only know about the subclass once it''s read the list.rb model > declaration)I Might Be Wrong, but I don''t think you don''t need to do this. Just name the file static_list.rb -- active_support overrides Object.const_missing so that constant references get snake-cased and require''d. Devin
Matthew Beale
2006-Apr-03 18:12 UTC
[Rails] Re: Is old school STI broken? How would you do the same now?
Chris T wrote:> Have you declared the list model in application.rb? > > class ApplicationController < ActionController::Base > model :listright, models/list.rb is definitly getting loaded, I run tests on it, I have been using it to create, that''s not a problem. As far as I recall (though i have not dug through the current rails code to be sure) every class in list.rb will be loaded. I could throw foobarfoo.rb in the models directory and it''s contents would be loaded.> You need to do this so the application knows about StaticList class (It > will only know about the subclass once it''s read the list.rb model > declaration)Well, once it reads the list.rb file yeah, which it is doing. Hence my own confusion :-) -- Posted via http://www.ruby-forum.com/.
Matthew Beale
2006-Apr-03 18:12 UTC
[Rails] Re: Is old school STI broken? How would you do the same now?
Chris T wrote:> Have you declared the list model in application.rb? > > class ApplicationController < ActionController::Base > model :listright, models/list.rb is definitly getting loaded, I run tests on it, I have been using it to create, that''s not a problem. As far as I recall (though i have not dug through the current rails code to be sure) every class in list.rb will be loaded. I could throw foobarfoo.rb in the models directory and it''s contents would be loaded.> You need to do this so the application knows about StaticList class (It > will only know about the subclass once it''s read the list.rb model > declaration)Well, once it reads the list.rb file yeah, which it is doing. Hence my own confusion :-) -- Matthew Beale :: mixonic@synitech.com Resume & Portfolio @ http://madhatted.com -- Posted via http://www.ruby-forum.com/.
Rob Biedenharn
2006-Apr-04 14:12 UTC
[Rails] Re: Is old school STI broken? How would you do the same now?
for the magic to happen, you need TWO files. One called list.rb defines List and one called static_list.rb that defines StaticList. The spells don''t work if you don''t pronounce them properly. ;-) -Rob Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com +1 513-295-4739 On Apr 3, 2006, at 2:12 PM, Matthew Beale wrote:> Chris T wrote: >> Have you declared the list model in application.rb? >> >> class ApplicationController < ActionController::Base >> model :list > > right, models/list.rb is definitly getting loaded, I run tests on > it, I > have been using it to create, that''s not a problem. As far as I > recall > (though i have not dug through the current rails code to be sure) > every > class in list.rb will be loaded. I could throw foobarfoo.rb in the > models directory and it''s contents would be loaded. > >> You need to do this so the application knows about StaticList >> class (It >> will only know about the subclass once it''s read the list.rb model >> declaration) > > Well, once it reads the list.rb file yeah, which it is doing. > Hence my > own confusion :-) > > -- > Matthew Beale :: mixonic@synitech.com > Resume & Portfolio @ http://madhatted.com > > > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails