Forum: Ruby on Rails const_missing

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.
951ab869b77f78e21a4f0885d3e3292e?d=identicon&s=25 Andrew Lienhard (heights008)
on 2008-10-04 21:37
I've run into what I suspect is a total newbie error. Any help
appreciated!

I have a mysql db table called "content".

I created a model rb class called app/models/content.rb

class Content < ActiveRecord::Base
end

I setup a controller file like this:

class NewsController < ApplicationController
        def index
                @article=Content.find(:all)
        end
end

And I have a simple index view file, views/index.rhtml

I hit the url:
http://localhost:3000/news/index/

and I get this meaningless error:

/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/dependencies.rb:493:in
`const_missing'
app/controllers/news_controller.rb:3:in `index'

My config/database.yml is set to point to my local mysql db.

Ruby version    1.8.6 (i686-darwin8.11.1)
RubyGems version    1.3.0
Rails version    2.1.1
Active Record version    2.1.1
Action Pack version    2.1.1
Active Resource version    2.1.1
Action Mailer version    2.1.1
Active Support version    2.1.1
Application root    /Users/andrew/dev/ruby/myapps/jazzhouston
Environment    development
Database adapter    mysql
Database schema version    0

Any ideas?
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-10-04 21:43
(Received via mailing list)
On Oct 4, 8:37 pm, Andrew Lienhard <rails-mailing-l...@andreas-s.net>
wrote:
> I've run into what I suspect is a total newbie error. Any help
> appreciated!
>
> I have a mysql db table called "content".
>
> I created a model rb class called app/models/content.rb
>
for a class called Content, the table name should be contents

>
> And I have a simple index view file, views/index.rhtml
>
> I hit the url:http://localhost:3000/news/index/
>
> and I get this meaningless error:
>
> /usr/local/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/de 
pendencies.rb:493:in
> `const_missing'
> app/controllers/news_controller.rb:3:in `index'
>

That means that when it got to line 3 in your controller it hit a
constant it couldn't fine (Content by the looks of it). Not sure why,
given the information you've given. The full error trace might have
more info, it could also indicate that something bad happened while
loading content.db. If that is the case then running script/console
and evaluating just
Content
should give a more helpful error.

Fred
951ab869b77f78e21a4f0885d3e3292e?d=identicon&s=25 Andrew Lienhard (heights008)
on 2008-10-04 21:56
> for a class called Content, the table name should be contents

Thanks -- I just found the details on how to handle that:

class Content < ActiveRecord::Base
  set_table_name "content"
  set_primary_key "content_id"
end

(Note: Do I need to bounce the server (I did) anytime a model change
happens?)

My database.yml looks like this:

development:
  adapter: mysql
  database: mydb
  username: myuser
  password: mypass
  host: localhost

And the same for test and production

Running the console gives this:

MacBook-Pro:~/dev/ruby/myapps/jazzhouston$ script/console
Loading development environment (Rails 2.1.1)
Consol>> Content
NameError: uninitialized constant Content
        from
/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/dependencies.rb:279:in
`load_missing_constant'
        from
/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/dependencies.rb:468:in
`const_missing'
        from
/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/dependencies.rb:480:in
`const_missing'
        from (irb):1
>>


Any ideas?

Thanks!
951ab869b77f78e21a4f0885d3e3292e?d=identicon&s=25 Andrew Lienhard (heights008)
on 2008-10-04 22:18
Well every mistake teaches you something, and I definitely learned a bit
about RoR today!

The problem? A typo! I had my model file named content.db instead of
content.rb

I found this by debugging inside of lib/active_support/dependencies.rb

Duh. Thanks again.
This topic is locked and can not be replied to.