Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to validate associated model id?

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?

like image 777
code4j Avatar asked Sep 16 '12 22:09

code4j


2 Answers

You should look into creating a custom validation method:

class Student < ActiveRecord::Base
  validates :course_id, presence: true, numericality: { only_integer: true }
  ...
  validate :validate_course_id

private

  def validate_course_id
    errors.add(:course_id, "is invalid") unless Course.exists?(self.course_id)
  end
end

First, your model will make sure that the course_id is a valid integer, and then your custom validation will make sure that the course exists in the database.

like image 89
Chris Peters Avatar answered Oct 08 '22 18:10

Chris Peters


You can validate the presence of the course association. So, if does not exists, valid? will be false.

Your class should be:

class Student < ActiveRecord::Base
  attr_accessible :course_id, :name, :password, :status, :studentID, :year
  belongs_to :course

  validates :course, :name, :password, :status, :studentID, :year, presence: true
end

The differences of validating foreign key and the association itself are explained at this post.

If you don't need to validate presence of course, you can do this workaround:

validates :course, presence: true, if: -> {course_id.present?}
like image 45
William Weckl Avatar answered Oct 08 '22 20:10

William Weckl