Advertisement
Guest User

Untitled

a guest
Mar 31st, 2015
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. class Movie < ActiveRecord::Base
  2. # Callbacks & Plugins
  3.  
  4. # Associations
  5. has_and_belongs_to_many :categories
  6. has_many :ratings
  7.  
  8. # Validations
  9. validates :name, presence: true, uniqueness: true
  10. validates :description, presence: true
  11.  
  12. # Scopes
  13. scope :category, -> (category) { joins(:categories).where("categories.id = ?", category) }
  14. scope :searchable, -> (query) { where("name LIKE '%?%'", query) }
  15. scope :rating, -> (rating) { joins(:ratings).average("ratings.value")) }
  16. end
  17.  
  18. class Rating < ActiveRecord::Base
  19. # Callback & plugins
  20.  
  21. # Associations
  22. belongs_to :user
  23. belongs_to :movie, counter_cache: true
  24.  
  25. # Validations
  26. validates :value, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 1, less_than_or_equal_to: 5 }
  27. validates :user, presence: true, uniqueness: { scope: :movie_id }
  28. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement