Forum: Ruby on Rails Database constraints

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Mark N. (Guest)
on 2006-12-28 23:35
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,

Anthony E. (Guest)
on 2006-12-29 06:40
(Received via mailing list)

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. <removed_email_address@domain.invalid> wrote:
> end
>   end
> it's a good idea but that's my humble opinion.
> Beste regards,
> Mark
> --
> Posted via
> >

Cell: 808 782-5046
Current Location: Melbourne, FL
This topic is locked and can not be replied to.