GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Nested forms validate uniqueness based on database before transaction · Issue #356 · ryanb/nested_form

This repository was archived by the owner on Dec 12, 2021. It is now read-only.
This repository was archived by the owner on Dec 12, 2021. It is now read-only.

Nested forms validate uniqueness based on database before transaction #356

Copy link
Copy link
Open
Open
Nested forms validate uniqueness based on database before transaction#356
Copy link

Description

@onebree
onebree
opened on Jul 9, 2015
Issue body actions

Rails 4.2.0, nested_form 0.3.2

TL;DR - The validates_uniqueness_on method only compares current records at their current state before the form was submitted.

Location has_many :dids
Location has_many :screened_dids
Location accepts_nested_attributes_for :screened_dids

Did has_one :screened_did

ScreenedDid belongs_to :location
ScreenedDid belongs_to :did
ScreenedDid validates_uniqueness_of :did_id

Given the above associations, say a Location has 5 DIDs and 0 Screened DIDs. On the nested form, I add 5 rows of Screened DIDs, all having the same selected :did_id. Because there were zero Screened DID records for this location before the transaction, all nested objects will be created with different ids but the same :did_id.

If I have 1 Screened DID present with a did_id: 500, and I attempt to create/update one other object with the same, the validation will kick in.

If I have 2 Screened DID objects with different :did_id (500 and 501), and I update both to have the same (502), then the validation will fail, like in the first scenario.

Say in one transaction I attempt to delete ScreenDID id: 1 and create a second record with the :did_id of the first. The form is bounced back, because the :did_id was already present before the transaction, even if the first record was supposed to be deleted.

Finally, if two records are saved in this manner, and are unchanged on the next form update, the two original records will not vall invalid uniqueness. In theory, it should since the objects were in the database before the transaction. I think the validation does not work because they were simply submitted already, and are for some reason exempt from comparing each other.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402792 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.