Render error message for NULL object

I’ve got an student_id column in my Attendance table. The ID is found by
searching with the student’s IDN in the before_create method:

class Attendance < ActiveRecord::Base
belongs_to :student

validates_numericality_of :student_id, :only_integer => true

def before_create
self.student = Student.find_by_idn(student_id)
end
end

This works just fine as long as the IDN that is entered is actually in
the Student table. If it is not, I get an error stating student_id is
NULL. How would I display “Student IDN not found” as an error message in
my view? I’m new to programming and tried this but it does not work:

def before_create
s = Student.find_by_idn(student_id)
if s = nil
errors.add(:student_id, “is not a valid IDN”)
else
self.student = s
end
end
end

Anyone have suggestions? Thanks for any help.