Reputation: 1473
I'm trying to create an enum using 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
I would be using this in a method in the User class:
def vote_on(bill, value) bill.votes.create(:value => value, :user_id => self.id) end
I was thinking about setting a validation that limits to :aye, :nay, :abstain, but it seems there is probably a better way to do this.
Upvotes: 2
Views: 3856
Reputation: 65877
No, its a better & clean way. I preferred symbols over strings when you have a known set of values to be processed. Since mongo db doesn't have a symbol type it will be stored as a string.
And mongoid will take care of the conversion. Also symbols have slight advantage over strings. check out the article for more info.
Upvotes: 3