Topic: Validation vs Foreign Keys [Solved]

I am new to rails but I have some exp. with Ruby.  I am working on a program that will catalog a collection, I have certain tables setup that will just store values such as author, publisher, artist, etc.  They are not user writable.  Normally I would put constraints in the user tables for this such as references or foreign key but the rails documentation stress keeping the logic in the head not in the db so I looked at performing these checks using validations.  My question is, is it possible and plausible to take user data from a form, validate it against a pre-built table and if valid enter it into the user table or give the user an error if invalid?  Should I use a foreign key plugin or execute raw sql for this to ensure integrity or will validation be sufficient?  The other question I have is where to put the code.  The documentation says to keep the controllers skinny, so I would tend to put this in the model, but doesn't the controller handle the incoming input?

Thanks
Matty

Last edited by matty (2011-04-27 09:49:35)

Re: Validation vs Foreign Keys [Solved]

hi matty. I just want to pick up on a couple of points that suggest to me that you have been mislead in some way or you have misunderstood something

rails documentation stress keeping the logic in the head not in the db

That's not strictly true. Rails stresses that validations and businss logic should be kept firmly in the model or middle tier layer this is mainly because programmers are lazy and undisciplined and will dot their business logic around their controllers, in their views and anywhere else it is possbile to put it rather than in model where it firmly belongs.
to the absolute contrary of your impression It is extremely good practice and very desirable to have your database stucture back up your business logic with fk constraints, indexes etc... so do both smile

is it possible and plausible to take user data from a form, validate it against a pre-built table and if valid enter it into the user table or give the user an error if invalid?

Yes, and very simple to do.
use the user model directly and write some custom validation routines to validate the input data against your standing data. See here for the best approach to this
http://www.perfectline.ee/blog/building … validators

The other question I have is where to put the code.  The documentation says to keep the controllers skinny, so I would tend to put this in the model, but doesn't the controller handle the incoming input?

hope my last answer covers this.
forms are posted back to the server. your routes.rb file dtermines what should be done with the url that your server receives and in this case it's likely going to be a PUIT or a POST request to the user controller which means that if you are using restfull routes your update or create actions will be called.
These have the responsibilitiy of marshalling the data into the right objects and rendering the correct response back to the browser. The objects have the responsibility of validating any data and raising errors if there are any problems. the controller action in it's most basic form will render errors or redirect to an appropriate action dep[ending on the result of a call to create/update a user model in your instance.

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: Validation vs Foreign Keys [Solved]

Thanks for the tips.  My instinct was to use both validation and db constraints, so I guess that solves that.  As to where to put the validation code, I will put it in the model as it pertains to the data, which is what my instinct was as well.

Matty