STI isn't working for me

i am trying to use single table inheritance for the first time and not
having much luck so far.

in my model i have:

class NavigationItem < ActiveRecord::Base
end

class MenuItem < NavigationItem
end

class LinkItem < NavigationItem
end

but when i use the console to create a new record like menu =
MenuItem.new i get an error saying: NameError: uninitialized constant
MenuItem

am i doing something wrong?

Hey Josh,
Have a look at the following wiki;
http://wiki.rubyonrails.org/rails/pages/SingleTableInheritance,
specifically the ‘note’ part.* “Note:* Rails must â??seeâ?? the file
containing the STI classes before it can use them, otherwise youâ??ll end
up with a â??uninitialized constantâ? error. This can be a problem if the
name of the class and the name of the file defining it are not the same,
e.g. you have the model â??Managerâ?? in the file â??employees.rbâ??. Rails will
not be able to divine the filename from the class name in this case.”

I take it that this is what is biting you :)
Regards
Stef

yes, that is what i was doing. so what is the better practice? on the
wiki it suggests adding â??model :employeesâ? to the application.rb, but it
sounds like i could do that or just put the classes in separate files
that have matching names?

Stef Telford wrote:

Hey Josh,
Have a look at the following wiki;
http://wiki.rubyonrails.org/rails/pages/SingleTableInheritance,
specifically the ‘note’ part.* “Note:* Rails must â??seeâ?? the file
containing the STI classes before it can use them, otherwise youâ??ll end
up with a â??uninitialized constantâ? error. This can be a problem if the
name of the class and the name of the file defining it are not the same,
e.g. you have the model â??Managerâ?? in the file â??employees.rbâ??. Rails will
not be able to divine the filename from the class name in this case.”

I take it that this is what is biting you :)
Regards
Stef

Josh K. wrote:

yes, that is what i was doing. so what is the better practice? on the
wiki it suggests adding “model :employees” to the application.rb, but it
sounds like i could do that or just put the classes in separate files
that have matching names?

Yes put them in separate files with matching names. One model per file
please:)

Peter

Hey Josh,
Well, personally, I am all for thinking about the person that comes
-after- you (so to speak). Putting them into seperate model files is
probably ‘saner’ in my view. Even if your only doing this on your own
application, its probably a good habit to get into … but … thats jst
my 2cents and all coding style is highly subjective. I would hate to
come across as a pythonista :stuck_out_tongue_winking_eye: :stuck_out_tongue:

Regards
Stef