class PostImplication < ApplicationRecord
  self.primary_key = :post_id, :parent_post_id

  belongs_to :post, inverse_of: :parent_post_implications
  belongs_to :parent_post, class_name: 'Post', inverse_of: :child_post_implications

  validates :post_id, presence: true, uniqueness: { scope: :parent_post_id }
  validates :parent_post_id, presence: true

  validate :parent_post_mustnt_be_itself

  private

  def parent_post_mustnt_be_itself
    if parent_post_id == post_id
      errors.add :parent_post_id, '親投稿に同じ投稿を設定することはできません．'
    end
  end
end
