I have a class similar to the following:
class FruitKinds < ActiveRecord::Base
Apple = FruitKinds.find(:all).find { |fk|
fk.fruit_name == :apple.to_s
}
# ... other fruits
# no methods
end
Apple and other specific Fruits are commonly used as default values elsewhere in my application, so I want a handy means to refer to them in an enumerable, static-ish way.
However, there's a problem. There is a database migration to create the FruitKinds
table and populate it with the special Fruits
like Apple
. When the database migration runs to initialize FruitKinds, rake
fails to start because it first loads FruitKinds
, which then makes a call to the database, which of course fails since the FruitKinds table is not yet there.
The workaround is to comment out the FruitKinds::* fields while the migration runs, but that is awful and hacky. What's the best way to do this?