Skip to content

Instantly share code, notes, and snippets.

@cypok
Created February 1, 2009 08:45
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 cypok/55825 to your computer and use it in GitHub Desktop.
Save cypok/55825 to your computer and use it in GitHub Desktop.
class Rate < ActiveRecord::Base
belongs_to :user
belongs_to :book
validates_presence_of :user, :book, :value
validates_numericality_of :value,
:only_integer => true
validates_inclusion_of :value,
:in => 1..10,
:message => "should be from 1 to 10"
validate :uniqueness_of_user
def uniqueness_of_user
if user and book
errors.add_to_base :message => "You have already rated this book." if
Rate.count( :conditions => {:user_id => user.id, :book_id => book.id} ) != 0
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment