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.
Matthew B. (Guest)
on 2006-04-03 20: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 B. :: removed_email_address@domain.invalid
Resume & Portfolio @ http://madhatted.com
Chris T (Guest)
on 2006-04-03 21: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
Matthew B. (Guest)
on 2006-04-03 22: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 B. (Guest)
on 2006-04-03 22: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 B. :: removed_email_address@domain.invalid
Resume & Portfolio @ http://madhatted.com
Rob B. (Guest)
on 2006-04-04 18: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 B.		http://agileconsultingllc.com
removed_email_address@domain.invalid
+1 513-295-4739
This topic is locked and can not be replied to.