class Child < ActiveRecord::Base has_many :parenthoods has_many :parents, through: :parenthoods, source: :person validates :first_name, presence: true validates :last_name, presence: true scope :not_related_to, lambda { |parent_id| joins("LEFT JOIN ( SELECT child_id FROM parenthoods WHERE person_id = #{sanitize(parent_id)} ) as s1 on s1.child_id = children.id") .where("s1.child_id is null") .order(:first_name, :last_name) .uniq } def name "#{first_name} #{last_name}" end end