I have a student and a course model. Student belongs to course, and course has many students.
class Student < ActiveRecord::Base
attr_accessible :course_id,
You can just do a custom validation method that checks the validity of your course id:
# course.rb
validates :course_id_is_valid
def course_id_is_valid
if #course id isn't valid
errors.add(:course_id, "is invalid")
end
end
Alternately, if the course list is a discrete list, you can use the :in option on validations:
# course.rb
validates :course_id, :inclusion => { :in => list_of_courses, :message => "%{value} is not a course id" }
There are more options. Both of these examples come from the Rails docs:
http://guides.rubyonrails.org/active_record_validations_callbacks.html#custom-methods