1

I am using Ruby v1.9.2 and Ruby on Rails v3.2.2. I have many model classes having constant statements. For instance:

# app/models/class_one.rb
class ClassOne < ActiveRecord::Base
  CONSTANT_ONE = ClassTwo::CONSTANT_TWO
end

# app/models/class_two.rb
class ClassTwo < ActiveRecord::Base
  CONSTANT_TWO = 1
end

When I restart the server, I get the following error:

Routing Error
uninitialized constant ClassTwo::CONSTANT_TWO
Try running rake routes for more information on available routes. 

Is the error related to the loading order of files (and so of classes)? How should I solve the problem?

Note: Since Ruby on Rails, I heard that a "working" solution could be to state constants in initializer files (in the config/initializers/ directory). If so, how should that be made the proper way? What do you think about?

user12882
  • 4,702
  • 9
  • 39
  • 54

2 Answers2

0

Those 2 classes are defined in the same file? wow. Reorder the classes:

class ClassTwo < ActiveRecord::Base
  CONSTANT_TWO = 1
end

class ClassOne < ActiveRecord::Base
  CONSTANT_ONE = ClassTwo::CONSTANT_TWO
end

should fix it. CONSTANT_ONE = ClassTwo::CONSTANT_TWO is evaluated as soon as it's parsed.

  • Classes are *not* defined in the same file. I updated the question in order to specify that. Be very calm. – user12882 Nov 08 '12 at 23:50
  • a **require 'class_two'** as the first line of the class_one.rb would do then :) –  Nov 08 '12 at 23:51
  • Since Ruby on Rails, I heard that a solution could be that of stating constants in initializer files (in the `config/initializers/` directory). What do you think about? – user12882 Nov 08 '12 at 23:54
  • sure, should work with the dependency loading mechanism of rails doing stuff behind your back. I like to explicitly state my dependencies though. –  Nov 08 '12 at 23:57
  • More than *working*, is it a *common* solution to the issue? That is, *in my case related to constants*, is the "initializer approach" commonly used "on implementing a RoR application" / "by RoR developers"? – user12882 Nov 09 '12 at 00:24
0

Constants in Rails are kind of a pain, as you are beginning to find out. The pain only increases as you really dig in. It is much easier and more maintainable to use an actual method on the class than to use a constant. For example, in testing, it is MUCH easier to modify a method than a constant when covering a variety of use cases. Also, when doing more complicated programming, you can begin to get into loading issues (like multiple loading errors, or unavailability, like you have) that just don't happen with methods. I've stopped using constants in my Rails apps altogether, and haven't missed them a bit. You may be interested in an article that Advi Grimm wrote to the same effect.

Edit:

If you really desire to use constants, in the way you described, check out Where's the best place to define a constant in a Ruby on Rails application? for more info.

Community
  • 1
  • 1
Brad Werth
  • 17,411
  • 10
  • 63
  • 88
  • However, in testing, in order to make things "MUCH easier", you could be more explicit (by stating the full name of constants) instead of using methods. – user12882 Nov 09 '12 at 01:17
  • In my case related to constants, is the "initializer approach" (mentioned in your liked question "[Where's the best place to define a constant in a Ruby on Rails application?](http://stackoverflow.com/questions/1107782/wheres-the-best-place-to-define-a-constant-in-a-ruby-on-rails-application)") commonly used "on implementing a RoR application" / "by RoR developers"? – user12882 Nov 09 '12 at 01:20
  • The use case I was thinking of would be something like (trivially easy example) if the constant was set to true, and you wanted to verify in a test that it would fail if adjusted to false. This type of thing comes up all the time in real-world examples much more complicated than my quick example (like per-page count, for pagination, for example), but hopefully you get the point. This kind of thing is almost impossible to do satisfactorily using constants. – Brad Werth Nov 09 '12 at 01:21
  • I would say that the approach described in the other answer is what is commonly considered to be best-practice. – Brad Werth Nov 09 '12 at 01:22
  • When using constants, that is. – Brad Werth Nov 09 '12 at 01:23