-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost.rb
More file actions
38 lines (28 loc) · 1022 Bytes
/
post.rb
File metadata and controls
38 lines (28 loc) · 1022 Bytes
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
32
33
34
35
36
37
38
# frozen_string_literal: true
# - Create a post:
# Post.create! title: 'A post', author: Author.first
# Post.last.tags << Tag.last
class Post < ApplicationRecord
enum :state, available: 0, unavailable: 1, arriving: 2
belongs_to :author, inverse_of: :posts, autosave: true
# with autosave if you change an attribute using the association, calling a save on parent will propagate the changes
has_one :author_profile, through: :author, source: :profile
has_many :post_tags, inverse_of: :post, dependent: :destroy
has_many :tags, through: :post_tags
validates :title, allow_blank: false, presence: true
scope :published, -> { where(published: true) }
scope :recents, -> { where('created_at > ?', Time.current - 1.week) }
# # override a field - can be dangerous
# def title
# "<<<#{super}>>>"
# end
def short_title(**args)
title.truncate(args[:count] || 10)
end
def upper_title
title.upcase
end
def old_method
ActiveRecord::Base.allow_unsafe_raw_sql = true
end
end