class student < ActiveRecord::Base
has_many :projects
def has_a_teacher_by_the_name_of(name)
self.projects.any? { |project| project.teacher.exists?(:name => name) }
end
end
class project < ActiveRecord::Base
belongs_to :student
has_one :teacher
end
class teacher < ActiveRecord::Base
belongs_to :project
end
This doesn't work because a project might not have a teacher yet, so project.teacher throws an error:
You have a nil object when you didn't expect it! The error occurred while evaluating nil.exists?