parsley/app/models/log.rb

25 lines
723 B
Ruby
Raw Normal View History

2016-07-06 21:00:35 -05:00
class Log < ActiveRecord::Base
belongs_to :recipe
belongs_to :source_recipe, class_name: 'Recipe'
belongs_to :user
validates :date, presence: true
validates :user_id, presence: true
2016-07-27 22:30:57 -05:00
validates :rating, numericality: { only_integer: true, allow_blank: true, greater_than_or_equal_to: 1, less_than_or_equal_to: 5, message: 'must be an integer between 1 and 5, inclusive' }
2016-07-06 21:00:35 -05:00
2016-09-28 17:08:43 -05:00
scope :for_user, ->(user) { where(user_id: user) }
scope :for_recipe, ->(recipe) { where(source_recipe_id: recipe) }
2016-07-06 21:00:35 -05:00
2016-07-27 22:30:57 -05:00
accepts_nested_attributes_for :recipe, update_only: true, allow_destroy: false
2016-09-28 17:08:43 -05:00
after_save :update_rating
def update_rating
if self.source_recipe
self.source_recipe.update_rating!
end
end
2016-07-06 21:00:35 -05:00
end