Blogs
This commit is contained in:
30
app/models/blog.rb
Normal file
30
app/models/blog.rb
Normal file
@ -0,0 +1,30 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# == Schema Information
|
||||
#
|
||||
# Table name: blogs
|
||||
#
|
||||
# id :integer not null, primary key
|
||||
# article :text not null
|
||||
# published_date :string default(""), not null
|
||||
# title :string not null
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
# user_id :integer
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
# index_blogs_on_user_id (user_id)
|
||||
#
|
||||
|
||||
class Blog < ApplicationRecord
|
||||
belongs_to :author, class_name: "User", foreign_key: :user_id, inverse_of: :blogs
|
||||
|
||||
scope :published, -> { where("published_date <= ?", Time.zone.now) }
|
||||
|
||||
def published?
|
||||
return false if published_date.empty?
|
||||
|
||||
Time.zone.parse(published_date) <= Time.zone.now
|
||||
end
|
||||
end
|
@ -20,6 +20,8 @@
|
||||
class User < ApplicationRecord
|
||||
has_secure_password
|
||||
|
||||
has_many :blogs, dependent: :destroy
|
||||
|
||||
validates :display_name, presence: true
|
||||
validates :email, presence: true, email_format: true, uniqueness: true
|
||||
validates :password_confirmation, presence: true, if: ->(m) { m.password.present? }
|
||||
|
Reference in New Issue
Block a user