Forum: Ruby on Rails Is old school STI broken? How would you do the same now?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
39b36b2be47228f8619d61ea7a607a25?d=identicon&s=25 Matthew Beale (mixonic)
on 2006-04-03 18:57
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
58c6efb8466b9f85155fe6aa9fc37fce?d=identicon&s=25 Chris T (Guest)
on 2006-04-03 19:31
(Received via mailing list)
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
39b36b2be47228f8619d61ea7a607a25?d=identicon&s=25 Matthew Beale (mixonic)
on 2006-04-03 20:12
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 :-)
39b36b2be47228f8619d61ea7a607a25?d=identicon&s=25 Matthew Beale (mixonic)
on 2006-04-03 20:12
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
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2006-04-04 16:18
(Received via mailing list)
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
This topic is locked and can not be replied to.