Forum: Ruby on Rails error: uninitialized constant

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.
10c741d575988433e3fcd04133f3c462?d=identicon&s=25 Sebastian Friedrich (Guest)
on 2006-03-01 08:18
(Received via mailing list)
total rails nuby here. I have a table "thumbnails", hence the model
Thumbnail. I'm extending the Thumbnail class with a few constants
needed for thumbnail cropping:

class Thumbnail < ActiveRecord::Base

SOURCE_FILE_PATH = "path/to/source"
TARGET_FILE_PATH = "path/to/target"
TARGET_WIDTH = 100
TARGET_HEIGHT = 100

end

Now, in my controller i'm trying to access these:

source_path = Thumbnail::SOURCE_FILE_PATH

however, i get an error:

uninitialized constant SOURCE_FILE_PATH
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/
active_support/dependencies.rb:200:in `const_missing'
This error occured while loading the following files:
source_file_path.rb

so, it seems it interprets the scope operator as a request for class
SOURCE_FILE_PATH in module Thumbnail. Doing this straight in Ruby
works fine, however. I've also seen a constant being called that way
from the View in the Agile book. anybody could enlighten me on what
is different here?

is this a bad way of dealing with "hardwired" data in general? should
this be in its own model even though its very much related to the
"thumbnail" logic? I'm still having a somewhat hard time to wrap by
head around the various concepts of separation, so please forgive me
for sounding stupid. any good suggestions on how to incorporate that
the Rails-way.

thanks.
sebastian
833fbd3f58a8ebbeb43756e8634334ed?d=identicon&s=25 Nicholas Henry (Guest)
on 2006-04-07 15:25
(Received via mailing list)
Does any one have an answer to this, I would love to know!

MyModel::MyConstant give an initialization error. Why?

Cheers,
Nicholas
833fbd3f58a8ebbeb43756e8634334ed?d=identicon&s=25 Nicholas Henry (Guest)
on 2006-04-07 15:25
(Received via mailing list)
Sorry, MyModel::MYCONSTANT give an initialization error.
This topic is locked and can not be replied to.