Rails: Validating an array of ids

Mark Kerr

Application

I am working on a college admissions system where a student can make an application to up to 5 courses. The way I have designed this is to have an Application model and a CourseApplication model. An application can consist of many course_applications:

class Application < ActiveRecord::Base

  # Assosciations
  belongs_to :user
  has_many :course_applications, dependent: :destroy
  has_many :courses, through: :course_applications
  has_one :reference

  # Validations
  validates :course_applications, presence: true

end

Course Application

class CourseApplication < ActiveRecord::Base

  # Intersection entity between course and application.
  # Represents an application to a particular course, has an optional offer

  # Associations
  belongs_to :application
  belongs_to :course
  has_one :offer, dependent: :destroy

end

I want to make sure that a student cannot apply to the same course twice. I have already done some research but have had no success. This is the UI for a student making an application:

Screenshot of application form

When a course is selected, the course id is added to an array of course ids:

def application_params
  params.require(:application).permit(:user, course_ids: [])
end

Right now a student can select the same course twice, I want to prevent them from doing this. Any help is much appreciated.

Ropeney

For the rails side, I would do on the CourseApplication

validates :course, uniqueness: { scope: :application }

For your reference this can be found at: http://guides.rubyonrails.org/active_record_validations.html#uniqueness

Also suggest on the database side to make a migration

add_index :course_applications, [:course, :application], :unique => true

For the validating on the form you will have to write javascript to make sure two things are selected, this will just return an error when someone tries to do it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Rails validating values in array

From Dev

Rails Console find users by array of ids

From Dev

Get all Rails records from an array of IDs

From Dev

Send a jQuery array of ids to Rails 4

From Dev

Rails - How to query by multiple ids(array)

From Dev

Send a jQuery array of ids to Rails 4

From Dev

Rails Console find users by array of ids

From Dev

Validating associated models in Rails

From Dev

Validating minutes and seconds in Rails

From Dev

Rails validating enums

From Dev

Rails SQL query, passing array of ids to sql WHERE...IN clause

From Dev

Rails how to find record by association's ids contain array

From Dev

Saving an array of ids into a foreign key field - Rails 4

From Dev

pass an array of Ids back from a view to a rails controller

From Dev

Rails Postgres ActiveRecord match by intersection of ids and custom array in query

From Dev

Validating Array in Laravel

From Dev

Validating multiple files in array

From Dev

Validating a JSON array in Laravel

From Dev

Validating input based on array

From Dev

Validating multiple files in array

From Dev

Validating array request in Laravel

From Dev

Conditionally validating uniqueness of a boolean in Rails

From Dev

Validating a time zone is valid in rails

From Dev

Validating decimal scale in Ruby on Rails

From Dev

Conditionally validating uniqueness of a boolean in Rails

From Dev

Rails validating uniqueness of polymorphic association

From Dev

validating array params via function

From Dev

Validating Array Keys in Laravel 5.2

From Dev

validating array objects in active record