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, :name, :password, :status, :studentID, :year
belongs_to :course
validates :name, :password, :status, :studentID, :year, :presence =>true
validates_associated :course
end
class Course < ActiveRecord::Base
attr_accessible :courseCode, :courseName, :courseYr
validates :courseCode,:courseName,:courseYr, :presence => true
validates :courseCode,:courseYr, :uniqueness=>{:message=>"Cannot repeat the code"}
has_many :students
end
In the form used to create student record, I let the user enter the course ID.
<div class="field">
<%= f.label :course_id %><br />
<%= f.text_field :course_id %>
</div>
But I don't know how to validate the course_id
input by the user. The student model validation will not generate an error, even when I type a course ID that does not exist. How do I get it to show the error?