Datamapper: let me know why I cannot delete a record

I am customizing my db model using datamapper and dm-contraints. I have two models that have many different relationships, but when I try to destroy it, the only message I get is false .

Is it possible to get a datamapper to give me more feedback, what relation exactly is causing the problem?

+4
source share
3 answers

Using datamapper 1.2.1:

 def why_you_no_destroy? model preventing = [] model.send(:relationships).each do |relationship| next unless relationship.respond_to?(:enforce_destroy_constraint) preventing << relationship.name unless relationship.enforce_destroy_constraint(model) end preventing end 
+9
source

Unfortunately, the DM does not provide a way to communicate why the destroy failed.

In most cases, the destruction failed due to its associations. The DM has a mechanism to avoid orphaned entries.

To avoid such destruction, you can use dm restrictions ( https://github.com/datamapper/dm-constraints ) to configure links to foreign keys of the true database level, which protects by default, but can be set instead of cascading deletes .

 class List has n, :todos, :constraint => :destroy (or :destroy!) end 

Unfortunately, at present, only PostgreSQL and MySQL support dm restrictions.

For another database, you can first check all associations and delete them, and then delete the model.

+2
source

You can get DataMapper error information from

 model.destroy if model model.errors.each do |error| p error end end 

Sometimes this does not tell you anything, and in this case you can put your code inside the start / save block, for example.

 begin model.destroy rescue Exception => exc p exc end 
0
source

All Articles