Created
June 22, 2010 17:29
-
-
Save josefrichter/448789 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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