Automatic validation

I want to automate a validation on all (or almost all) my models. That validations should happen before every delete/destroy.

The goal is to stop the user from deleting a record if it has any 'has*' associations. I know that there are gems like 'acts_as_paranoid' that will keep the record in the DB while marking it as 'deleted', but that is not what I need here.

I already have in a module the initial code that checks for association records. The code is posted below for critique and in case anybody is interested. The code might change, though, based on requirements.

What I need is to know how to make all models run automatically the method ('dependencies_found?' in the code below) whenever a record is deleted without having to go to all models and include the module manually.

Any ideas?

Thanks in advance.

<code> module Dependable   def dependencies_found?     dependencies = self.class.reflect_on_all_associations.select {|a| a.macro.to_s[0, 3] == 'has'}     dependencies.any? {|d| eval "!self.#{d.name}.blank?"}   end end </code>

pepe wrote in post #955921:

I want to automate a validation on all (or almost all) my models. That validations should happen before every delete/destroy.

The goal is to stop the user from deleting a record if it has any 'has*' associations.

Use foreign key constraints in the database for this. The Foreigner gem will help manage these constraints.

Best,

Use foreign key constraints in the database for this. The Foreigner gem will help manage these constraints.

Yes, I think you're right and going with Referential Integrity should be the way to go to make sure the rules are enforced at the DB level. Although, I still would like to know how I can accomplish what I was trying to do.

I didn't know about Foreigner and took a look. It seems that it is meant to be used only with MySQL and PostgreSQL. I am using MSSQL and have not found anything useful out there as gems/plugins go for this DB. Any ideas?

pepe wrote in post #955955:

Use foreign key constraints in the database for this. The Foreigner gem will help manage these constraints.

Yes, I think you're right and going with Referential Integrity should be the way to go to make sure the rules are enforced at the DB level. Although, I still would like to know how I can accomplish what I was trying to do.

With foreign key constraints. There is really no reason to do it any other way.

I didn't know about Foreigner and took a look. It seems that it is meant to be used only with MySQL and PostgreSQL. I am using MSSQL and have not found anything useful out there as gems/plugins go for this DB. Any ideas?

Yes, apparently you missed my announcement a couple months ago of my fork of Foreigner with MS SQL Server support:

The gem is available as marnen-foreigner.

I think sparkfly-foreigner also has MS SQL Server support.

Best,

you can run a sql command in your migration to generate a foreign key constraint yourself, look at activerecord execute

this should give you an idea:

http://fdietz.wordpress.com/2008/08/03/migrations-and-foreign-key-handling/

> Yes, I think you're right and going with Referential Integrity should > be the way to go to make sure the rules are enforced at the DB level. > Although, I still would like to know how I can accomplish what I was > trying to do.

With foreign key constraints. There is really no reason to do it any other way.

But it would provide me with knowledge of Rails internals I don't have right now that might become useful in the future.

Yes, apparently you missed my announcement a couple months ago of my fork of Foreigner with MS SQL Server support:GitHub - marnen/foreigner: Adds foreign key helpers to migrations and correctly dumps foreign keys to schema.rb The gem is available as marnen-foreigner.

Thanks, will look into it.

> you can run a sql command in your migration to generate a foreign key > constraint yourself, look at activerecord execute

this should give you an idea:

http://fdietz.wordpress.com/2008/08/03/migrations-and-foreign-key-han

That's exactly what I did yesterday when I realized Foreigner worked only with MySQL and PostgreSQL and got it working but Foreigner seems to help you along the way and make things easier and more 'Railsy', which was what I was looking for.

Thanks for the input, though.

Rajinder Yadav wrote in post #956033:

have not found anything useful out there as gems/plugins go for this DB. Any ideas?

you can run a sql command in your migration to generate a foreign key constraint yourself, look at activerecord execute

But don't, because it will not be DB-agnostic and it will not appear in the schema file. Foreigner is your best bet.

-- Kind Regards, Rajinder Yadav | DevMentor.org | Do Good! ~ Share Freely

Best,

pepe wrote in post #956059:

> Yes, I think you're right and going with Referential Integrity should > be the way to go to make sure the rules are enforced at the DB level. > Although, I still would like to know how I can accomplish what I was > trying to do.

With foreign key constraints. There is really no reason to do it any other way.

But it would provide me with knowledge of Rails internals I don't have right now that might become useful in the future.

Well, looking through the source code is never a bad thing. Just bear in mind that the DB is in the best position to do data integrity checking.

Yes, apparently you missed my announcement a couple months ago of my fork of Foreigner with MS SQL Server support:GitHub - marnen/foreigner: Adds foreign key helpers to migrations and correctly dumps foreign keys to schema.rb The gem is available as marnen-foreigner.

Thanks, will look into it.

Let me know if you have any problems with it.

Best,

Yes, apparently you missed my announcement a couple months ago of my fork of Foreigner with MS SQL Server support:GitHub - marnen/foreigner: Adds foreign key helpers to migrations and correctly dumps foreign keys to schema.rb The gem is available as marnen-foreigner.

Got it working. Thanks for the tip. It would be great if 'schema.rb' got updated with the code for the FK constraints, though.

I found this (Tickets - Ruby on Rails - rails 4347-add-foreign-key-support-to-migrations-and-schemarb-dump) from the original creator of the gem you forked but I'm not sure how I would go about using the feature.

pepe wrote in post #956233:

Yes, apparently you missed my announcement a couple months ago of my fork of Foreigner with MS SQL Server support:GitHub - marnen/foreigner: Adds foreign key helpers to migrations and correctly dumps foreign keys to schema.rb The gem is available as marnen-foreigner.

Got it working. Thanks for the tip. It would be great if 'schema.rb' got updated with the code for the FK constraints, though.

That's what Foreigner does. Is it not working for you?

Best,

Rajinder Yadav wrote in post #956033:

have not found anything useful out there as gems/plugins go for this DB. Any ideas?

you can run a sql command in your migration to generate a foreign key constraint yourself, look at activerecord execute

But don't, because it will not be DB-agnostic and it will not appear in the schema file. Foreigner is your best bet.

Thanks for the info, took a look, did not know about Foreigner. Agree DB-agnostic is a good thing.

> Got it working. Thanks for the tip. It would be great if 'schema.rb' > got updated with the code for the FK constraints, though.

That's what Foreigner does. Is it not working for you?

My mistake. I just saw them at the end of the file. Thanks for everything, this is going to help a lot.