aboutsummaryrefslogtreecommitdiff
path: root/interfaces/ruby/models/comment.rb
blob: f7f9e1311be9a86c6c5320b735c65fc89c159153 (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
class Comment < ActiveRecord::Base
  belongs_to :post
  belongs_to :user, optional: true
  
  validates_presence_of :date
  validates_format_of :email, with: /@/
  validates_presence_of :body
  validate :user_or_guest
  
private

  def user_or_guest
    user.present? or (author.present? and email.present?)
  end
end

class CreateCommentTable < ActiveRecord::Migration[6.0]
  def change
    create_table :comments do |t|
      t.references :post, foreign_key: true, index: true, null: false
      t.references :user, foreign_key: true
      t.string :author
      t.string :email
      t.string :body, null: false
      t.timestamps
    end
  end
end