Database constraints

I searched the Ruby on Rails forum but I could not really find an answer
to my “problem”. I’ve got a Proposal like this

class Proposal < ActiveRecord::Base
def validate
errors.add_on_empty %w( client_surname client_firstname
postal_code city )

I’ve got a NOT NULL constraint on the postal_code column (= tinyint(4)
unsigned) in the proposals database table. In the create proposal form
in the view the value in the input field is by default 0.

This makes sence because the ProposalsController creates a new Proposal
like this:

def new
@proposal =

This would probably create a new proposal and set the value of
postal_code to 0. It I remove the NOT NULL constraint from the table
proposals then the input text field is empty.

Is there a way to keep the NOT NULL constraint on the postal code and
not show the default value 0 in the view?

This is probably again a discussion about keeping or removing constraint
like UNIQUE, NOT NULL, … in total from the database. I don’t think
it’s a good idea but that’s my humble opinion.

Beste regards,



The easiest thing to do would be to use a varchar column for the
postal code. This makes sense to me because postal codes can by
non-numeric (Canadian postal codes are a good example). Additionally
this would result in a blank rather than a 0.

Another point, if you just call this will not persist to the
database, thus a nil field should remain nil until you actually
persist and the data is loaded from the database.

Finally, I highly recommend using migrations if you are not already.
You can save yourself lots of time in the long run if you get used to


Anthony E.

On 12/28/06, Mark N. [email protected] wrote:

it’s a good idea but that’s my humble opinion.

Beste regards,


Posted via

Cell: 808 782-5046
Current Location: Melbourne, FL