Skip to content

Instantly share code, notes, and snippets.

@josefrichter
Created June 22, 2010 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josefrichter/448789 to your computer and use it in GitHub Desktop.
Save josefrichter/448789 to your computer and use it in GitHub Desktop.
class Suggestion
include Mongoid::Document
field :title, :type => String
field :body, :type => String
field :slug, :type => String
embeds_many :votes
embeds_many :features
#index :votes_difference
validates_presence_of :title, :body
validates_uniqueness_of :title, :slug
before_create :generate_slug
def votes_difference # virtual attribute, calculated dynamically
# find_all is a ruby method to filter array
self.votes.find_all{|vote| vote.value == true}.count - self.votes.find_all{|vote| vote.value == false}.count
# TODO doesn't this fire up too many database calls?
end
def to_param
slug
end
private
def generate_slug
self.slug = title.parameterize # TODO validate uniqueness
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment