Forum: Ruby on Rails Database constraints

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
520c24bad2fa9b171d022e3600cd5e58?d=identicon&s=25 Mark Noten (mnoten)
on 2006-12-28 22: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
  protected
    def validate
      errors.add_on_empty %w( client_surname client_firstname
postal_code city )
    end
end

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 = Proposal.new
  end

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,

Mark
31254903db793bf6f84bbd607fe092fd?d=identicon&s=25 Anthony Eden (Guest)
on 2006-12-29 05:40
(Received via mailing list)
Mark,

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 Model.new 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
them.

HTH.

V/r
Anthony Eden

On 12/28/06, Mark Noten <rails-mailing-list@andreas-s.net> wrote:
> end
>   end
> it's a good idea but that's my humble opinion.
>
> Beste regards,
>
> Mark
>
> --
> Posted via http://www.ruby-forum.com/.
>
> >
>


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