Skip to content

Validation failures when modifying collections #100

Description

@elliot-nelson

In our API, one of the primary models has several sub-model collections. These sub models all have validations, and the primary model also validates various aspects of the relationship between the collections. (For example: you can't save the Order if there isn't at least one Item. You can't have more Items than item_limit. Etc.)

When creating a record using representers, everything goes smoothly. However, when updating a record, these validations don't really work: even when using the find_or_instantiate strategy, it seems like the core call (somewhere along the line) is:

order.items = [new array of items]

Unfortunately, on an existing record, this is an instant call-to-action by Rails -- it runs off and inserts and deletes records, all before save is ever called on the parent object.

This behavior by Rails is one reason all our web forms use nested_attributes_for: the web forms are submitting the "items_attributes" collection, which means Rails doesn't attempt any database modifications until all the models are validated.

I saw another ticket (#81) where you mentioned that you shouldn't need to use nested_attributes with roar. Do you have any patterns that would allow roar to behave the way way nested_attributes does? (That is: that will not make any database updates until all of the models being touched have been validated.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions