I wanted to create an attribute on my model that took in a symbol for ascending or descending. However, RoR does not allow enumerated types in the database, so I created a boolean property and then added an attribute on my model. This works great for everything except validations, which I can’t seem to get to work. container.valid? always returns true, even when order hasn’t been set.Any ideas?
class Container < ActiveRecord::Base
attr_accessible :score_order
validates :order, :presence => true
def order
return order_ascends ? :ascending : :descending
end
def order=(order)
case order
when :ascending
self.order_ascends = true
when :descending
self.order_ascends = false
else
throw :unexpected_argument
end
end
end
You want to validate order_ascends:
The order method is just what you’re using to interface to order_ascends.