# Automatic validation

**URL:** <https://discuss.rubyonrails.org/t/automatic-validation/55235>\
**Category:** rubyonrails-talk\
**Created:** [October 20, 2010, 9:19pm UTC](https://discuss.rubyonrails.org/t/automatic-validation/55235 "2010-10-20T21:19:26Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![pepe1](https://avatars.discourse-cdn.com/v4/letter/p/dec6dc/32.png) [@pepe1](https://discuss.rubyonrails.org/u/pepe1)\
**Post date:** [October 20, 2010, 9:19pm UTC](https://discuss.rubyonrails.org/t/automatic-validation/55235/1 "2010-10-20T21:19:26Z")

</div>

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 &nbsp;&nbsp;def dependencies\_found? &nbsp;&nbsp;&nbsp;&nbsp;dependencies = self.class.reflect\_on\_all\_associations.select {|a| a.macro.to\_s[0, 3] == 'has'} &nbsp;&nbsp;&nbsp;&nbsp;dependencies.any? {|d| eval "!self.#{d.name}.blank?"} &nbsp;&nbsp;end end \</code\>

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [October 20, 2010, 9:26pm UTC](https://discuss.rubyonrails.org/t/automatic-validation/55235/2 "2010-10-20T21:26:37Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![pepe1](https://avatars.discourse-cdn.com/v4/letter/p/dec6dc/32.png) [@pepe1](https://discuss.rubyonrails.org/u/pepe1)\
**Post date:** [October 21, 2010, 12:46am UTC](https://discuss.rubyonrails.org/t/automatic-validation/55235/3 "2010-10-21T00:46:07Z")

</div>

> 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?

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [October 21, 2010, 5:54am UTC](https://discuss.rubyonrails.org/t/automatic-validation/55235/4 "2010-10-21T05:54:38Z")

</div>

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:

> **[GitHub - marnen/foreigner: Adds foreign key helpers to migrations and...](https://github.com/marnen/foreigner)**
>
> Adds foreign key helpers to migrations and correctly dumps foreign keys to schema.rb - GitHub - marnen/foreigner: Adds foreign key helpers to migrations and correctly dumps foreign keys to schema.rb

The gem is available as marnen-foreigner.

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

Best,

---

<div class="post-metadata">

**Author:** ![Dev\_Guy](https://avatars.discourse-cdn.com/v4/letter/d/ec9cab/32.png) [@Dev\_Guy](https://discuss.rubyonrails.org/u/Dev_Guy)\
**Post date:** [October 21, 2010, 10:31am UTC](https://discuss.rubyonrails.org/t/automatic-validation/55235/5 "2010-10-21T10:31:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Dev\_Guy](https://avatars.discourse-cdn.com/v4/letter/d/ec9cab/32.png) [@Dev\_Guy](https://discuss.rubyonrails.org/u/Dev_Guy)\
**Post date:** [October 21, 2010, 10:35am UTC](https://discuss.rubyonrails.org/t/automatic-validation/55235/6 "2010-10-21T10:35:05Z")

</div>

this should give you an idea:

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

---

<div class="post-metadata">

**Author:** ![pepe1](https://avatars.discourse-cdn.com/v4/letter/p/dec6dc/32.png) [@pepe1](https://discuss.rubyonrails.org/u/pepe1)\
**Post date:** [October 21, 2010, 12:37pm UTC](https://discuss.rubyonrails.org/t/automatic-validation/55235/7 "2010-10-21T12:37:47Z")

</div>

> \> 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](http://github.com/marnen/foreigner) The gem is available as marnen-foreigner.

Thanks, will look into it.

---

<div class="post-metadata">

**Author:** ![pepe1](https://avatars.discourse-cdn.com/v4/letter/p/dec6dc/32.png) [@pepe1](https://discuss.rubyonrails.org/u/pepe1)\
**Post date:** [October 21, 2010, 12:41pm UTC](https://discuss.rubyonrails.org/t/automatic-validation/55235/8 "2010-10-21T12:41:00Z")

</div>

> \> 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](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.

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [October 21, 2010, 12:43pm UTC](https://discuss.rubyonrails.org/t/automatic-validation/55235/9 "2010-10-21T12:43:17Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [October 21, 2010, 2:09pm UTC](https://discuss.rubyonrails.org/t/automatic-validation/55235/10 "2010-10-21T14:09:16Z")

</div>

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](http://github.com/marnen/foreigner) The gem is available as marnen-foreigner.
> 
> Thanks, will look into it.

Let me know if you have any problems with it.

Best,

---

<div class="post-metadata">

**Author:** ![pepe1](https://avatars.discourse-cdn.com/v4/letter/p/dec6dc/32.png) [@pepe1](https://discuss.rubyonrails.org/u/pepe1)\
**Post date:** [October 22, 2010, 4:40am UTC](https://discuss.rubyonrails.org/t/automatic-validation/55235/11 "2010-10-22T04:40:29Z")

</div>

> 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](http://github.com/marnen/foreigner) 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](https://rails.lighthouseapp.com/projects/8994/tickets/) 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.

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [October 22, 2010, 12:06pm UTC](https://discuss.rubyonrails.org/t/automatic-validation/55235/12 "2010-10-22T12:06:06Z")

</div>

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](http://github.com/marnen/foreigner) 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,

---

<div class="post-metadata">

**Author:** ![Dev\_Guy](https://avatars.discourse-cdn.com/v4/letter/d/ec9cab/32.png) [@Dev\_Guy](https://discuss.rubyonrails.org/u/Dev_Guy)\
**Post date:** [October 22, 2010, 9:53pm UTC](https://discuss.rubyonrails.org/t/automatic-validation/55235/13 "2010-10-22T21:53:22Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![pepe1](https://avatars.discourse-cdn.com/v4/letter/p/dec6dc/32.png) [@pepe1](https://discuss.rubyonrails.org/u/pepe1)\
**Post date:** [October 22, 2010, 10:00pm UTC](https://discuss.rubyonrails.org/t/automatic-validation/55235/14 "2010-10-22T22:00:52Z")

</div>

> \> 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.
