0.0
Low commit activity in last 3 years
A long-lived project that still receives updates
This gem converts the foreign key exceptions raised by the database into Active Model errors that can be displayed by an application.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0

Runtime

~> 7.0
 Project Readme

Unconstrained

Unconstrained for Active Record converts exceptions raised because of underlying foreign key constraints to appropriate ActiveModel::Errors

For example, if trying to insert a record in a model Book, which belongs_to :author using an invalid author_id, instead of getting an exception such as:

PG::ForeignKeyViolation: ERROR:  insert or update on table "books" 
violates foreign key constraint "fk_rails_xxxxxxxxxx"
DETAIL:  Key (author_id)=(xxxxxxxxxx) is not present in table "authors".

you will now get a validation error on the field author_id with the message "author is invalid"

Conversely, when if trying to delete an author but books exist, instead of:

PG::ForeignKeyViolation: ERROR:  update or delete on table "authors" 
violates foreign key constraint "fk_rails_xxxxxxxxxx" on table "books"
DETAIL:  Key (id)=(xxxxxxxxxx) is still referenced from table "books".

you will now get a base validation error on the record with the message "Cannot delete record because dependent books exist"

all tests

Usage

In your Gemfile:

gem 'unconstrained'

Notes

Only constraints that have to do with referential integrity are handled. This is intentional, as these require a roundrip to the database anyway. Constraints that can be enforced using validations in the application should be checked that way.

Currently only handles PostgreSQL.