diff options
Diffstat (limited to 'interfaces/ruby/models/comment.rb')
-rw-r--r-- | interfaces/ruby/models/comment.rb | 18 |
1 files changed, 13 insertions, 5 deletions
diff --git a/interfaces/ruby/models/comment.rb b/interfaces/ruby/models/comment.rb index b3977f6..f7f9e13 100644 --- a/interfaces/ruby/models/comment.rb +++ b/interfaces/ruby/models/comment.rb @@ -1,18 +1,26 @@ class Comment < ActiveRecord::Base belongs_to :post + belongs_to :user, optional: true + validates_presence_of :date - validates_presence_of :author - validates_presence_of :email 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 - t.string :author, null: false - t.string :email, null: false + 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 |