Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Movie < ActiveRecord::Base
- # Callbacks & Plugins
- # Associations
- has_and_belongs_to_many :categories
- has_many :ratings
- # Validations
- validates :name, presence: true, uniqueness: true
- validates :description, presence: true
- # Scopes
- scope :category, -> (category) { joins(:categories).where("categories.id = ?", category) }
- scope :searchable, -> (query) { where("name LIKE '%?%'", query) }
- scope :rating, -> (rating) { joins(:ratings).average("ratings.value")) }
- end
- class Rating < ActiveRecord::Base
- # Callback & plugins
- # Associations
- belongs_to :user
- belongs_to :movie, counter_cache: true
- # Validations
- validates :value, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 1, less_than_or_equal_to: 5 }
- validates :user, presence: true, uniqueness: { scope: :movie_id }
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement