我正在尝试使用mongoid创建一个枚举
class Vote
include Mongoid::Document
field :value, :type => Symbol # can be :aye, :nay, :abstain
#field :group_type, :type => Integer
belongs_to :user
embedded_in :bill
end
我将在User类的方法中使用它:
def vote_on(bill, value)
bill.votes.create(:value => value, :user_id => self.id)
end
我正在考虑设置一个限制为:aye,:nay,:abstain的验证,但似乎有更好的方法可以做到这一点 .
1 回答
不,这是一个更好,更干净的方式 . 当您有一组已知的值要处理时,我首选字符串 . 由于mongo db没有符号类型,因此它将存储为字符串 .
而mongoid将负责转换 . 符号也比字符串略有优势 . 查看article了解更多信息 .