Skip to content
This repository has been archived by the owner on Oct 19, 2018. It is now read-only.

records being saved without validation causing issues #101

Open
catmando opened this issue Jun 20, 2018 · 1 comment
Open

records being saved without validation causing issues #101

catmando opened this issue Jun 20, 2018 · 1 comment
Labels
Milestone

Comments

@catmando
Copy link
Contributor

HyperMesh does a temporary save of related records inside of a rolled back transaction without validations so that the client state is setup on the server. This is fine unless you have a Model that must not be saved if its invalid, then you have a problem.

Reported by @sfcgeorge

Will require either rethinking how we deal with unsaved client side changes, during validation.

@catmando catmando added the bug label Jun 20, 2018
@catmando catmando added this to the Release 0.15 milestone Jun 20, 2018
@sfcgeorge
Copy link
Contributor

1st issue we found is unique constraints: If you have a join table you may well have a compound unique constraint on it as we do below. For data integrity you also add a corresponding database unique constraint (the Rails validation isn't atomic). This new change causes the join table to be saved without validation, so you get an unfriendly database error instead of the nice Rails message.

validates :industry_id, uniqueness: { scope: :preference_id }

2nd issue is an off by one limit validation. The below used to let you save 3 then give you this error. Now, because the join table is saved before validations are run, it only lets you save 2.

validate :maximum_industries
def maximum_industries
  return unless preference.industries.count >= 3
  errors.add(:industries, "maximum 3 industries")
end

So when saving complex trees this causes all kinds of issues, so I don't think the change is tenable. I get that you want to build the tree of models on the server as it is on the client, then save. So why not do just that? build all the models and associations, then call save on the top level parent record, let Rails save all the associations and leaves as usual.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants