Is old school STI broken? How would you do the same now?


#1

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 B. :: removed_email_address@domain.invalid
Resume & Portfolio @ http://madhatted.com


#2

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


#3

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 :slight_smile:


#4

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 :slight_smile:


Matthew B. :: removed_email_address@domain.invalid
Resume & Portfolio @ http://madhatted.com


#5

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. :wink:

-Rob

Rob B. http://agileconsultingllc.com
removed_email_address@domain.invalid
+1 513-295-4739