0
私はおもちゃのアプリケーションを構築しようとしていて、解決できないような問題に遭遇しました。テーブルの値のペアが一意であることを強制するにはどうすればよいですか?値ペアに一意性を強制する複合キーを送信する
は、次のスキーマとします
create_table "courses", force: :cascade do |t|
t.string "title"
t.text "description"
t.string "number"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "status", default: 0
end
create_table "professors", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "status", default: 0
end
create_table "sections", force: :cascade do |t|
t.integer "number"
t.integer "max_enrollment"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "professor_id"
t.integer "course_id"
t.string "room"
t.index ["course_id"], name: "index_sections_on_course_id"
t.index ["professor_id"], name: "index_sections_on_professor_id"
end
を、私はcourse_id
とペアprofessor_id
は一意である必要がありますセクションテーブルの一意性制約を作成したかったです。私が掘り下げて見つけた唯一の事は、モデル内でvalidates
キーワードを使用して、単一フィールドの一意性を強制することができるということです... validates_with
というキーワードがありますが、バリデーターを書く方法が見つかりません私が探していることをする。どんな助けでも大歓迎です。