blob: 910528f8668d8117bda6fc31bedf15ffba449d34 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
class Creation < ApplicationRecord
validates :name, presence: true
validates :category_id, presence: true
belongs_to :user, counter_cache: true
belongs_to :category
has_many :photos, -> { order :created_at }, dependent: :destroy, as: :imageable
has_many :favorites, :dependent => :destroy
has_many :comments, dependent: :destroy
acts_as_taggable_on :tags
alias_method :author, :user
def to_param
"#{id}-#{name.parameterize}"
end
def primary_image
published? ? photos.sample : Photo.new
end
def published?
photos.any?
end
def is_liked_by(user)
favorites.where(user: user).any?
end
def liked_by(user)
favorites.find_or_create_by(user: user)
end
end
|