I would just use class methods:
class Order < ActiveRecord::Base
def self.test
123
end
def self.threshold
{ VIP: 500, PLATINUM: 20000 }
end
end
Constants would work, as well, but if you already have code that expects Order.test
and Order.threshold
to exist, you'd have to change your code to call the constant instead. Plus, Avdi Grimm gives some good reasons for using methods instead of constants in a blog post.
The reason accessing class variables won't work the way you expected is that Ruby restricts access to variables like this. You need to either define accessor methods directly (like self.test
or self.threshold
), or indirectly using something like cattr_reader
. You could also use cattr_accessor
if you need to write to the variables from the outside world.
I would generally recommend avoiding class variables, though. They have some unintuitive behavior.