I have the field ''LastName'' declared as NOT NULL in my table. Why Rails doesn''t add validate_presence_of :last_name in the model class, by default ? Bye. CP _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Carlos Pavia wrote:> I have the field ''LastName'' declared as NOT NULL in my table. > Why Rails doesn''t add > validate_presence_of :last_name > in the model class, by default ?Interesting. I have also wondered this, although I''m sure there''s a good reason for it. I''m doing even further validation on my data using PostgreSQL''s constraint functions. I''m sure it would be possible to extend ActiveRecord (or the generators) to deduce the necessary validations direct from the database, if they were specified, thus improving DRYness. Can anyone think of any reasons this would be a Very Bad Thing(tm)? ~Dave -- Dave Silvester Rent-A-Monkey Website Development Web: http://www.rentamonkey.com/
On 9/12/05 11:49 AM, "Dave Silvester" <dave-AJqNGCqIqVQ7cdpDWioORw@public.gmane.org> wrote:> Can anyone think of any reasons this would be a Very Bad Thing(tm)?I would not say it''s a Very Bad Thing, but there are issues. NOT NULL means you can not have a null value in the field. You can still however have an empty string in the field. On a recent project I remember we used NOT NULL for all string fields but not all of them had to contain data. We liked how they used empty strings because we could then do some special manipulation at the SQL level (though I forget the specifics as I type this). My personal preference would be to not implement ActiveRecord like this, and force people to manually make a decision of what they want to validate in the model. ~ Mike -- http://MikeZornek.com
Here's my view: Although it might make sense for AR to check the db for constraints and enforce them in the case of NOT NULL, for more complex constraints you'll want to do it in your model, not db schema. Besides, if you do NOT NULL, your app will go crazy if for some reason you have a null value, but will present a nice error message if you use AR's validation. Doing both doesn't make sense because you repeat yourself (it's not DRY). I do validation at the model level except of course for PKs/indicies and other performance-related stuff. But that's just my view. Jacob On 9/12/05, Mike Zornek <listguy@mikezornek.com> wrote:> On 9/12/05 11:49 AM, "Dave Silvester" <dave@rentamonkey.com> wrote: > > > Can anyone think of any reasons this would be a Very Bad Thing(tm)? > > I would not say it's a Very Bad Thing, but there are issues. > > NOT NULL means you can not have a null value in the field. You can still > however have an empty string in the field. On a recent project I remember we > used NOT NULL for all string fields but not all of them had to contain data. > We liked how they used empty strings because we could then do some special > manipulation at the SQL level (though I forget the specifics as I type > this). > > My personal preference would be to not implement ActiveRecord like this, and > force people to manually make a decision of what they want to validate in > the model. > > ~ Mike > -- > http://MikeZornek.com > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails