SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Aug 2006
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy Cannot create new record--all fields are null

    I have a form that has all the patient fields and it submits back to the appropriate controller action to initialize a new patient object and save the record.

    I am getting this exception:

    Code:
    Mysql::Error: Column 'weight' cannot be null: INSERT INTO patients (`address1`, `city`, `status`, `address2`, `prognosis`, `weight`, `zip_code`, `gender`, `height`, `date_of_birth`, `first_name`, `last_name`, `state`) VALUES(NULL, NULL, NULL, NULL, '', NULL, NULL, '', NULL, NULL, '', '', NULL)
    As you can see all values are NULL!

    This is the code that is generating the error:

    Code:
          
    p = Patient.new(params[:patient])
    p.save
    Before calling the save method, I even tried outputting all of the attributes and they all return the values I entered into the form. Where is it failing? Is it my rails installation?

    If I change all the table fields to allow null values, the patient record is saved (obviously completely blank).

    Also, to convince myself it is not me... I enabled the patient scaffold and tried to create a new patient through the generated form and it is giving the same exact error!

    Any ideas?

  2. #2
    SitePoint Addict
    Join Date
    Jan 2006
    Posts
    268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can you post your Patient model and the view code for the form?

    Thanks
    If you give someone a program,
    you will frustrate them for a day;
    if you teach them how to program,
    you will frustrate them for a lifetime.

  3. #3
    SitePoint Member
    Join Date
    Aug 2006
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Patient model as requested

    Patient Model:
    Code:
    class Patient < ActiveRecord::Base
      validates_presence_of :first_name, :last_name, :gender, :date_of_birth, :height, :weight, :prognosis
      
      attr_accessor :first_name, :last_name, :gender, 
                    :address1, :address2, :city, :state, :zip_code, 
                    :date_of_birth, :height, :weight,
                    :prognosis, :status
      
      PROGNOSIS_TYPES = [
          # Displayed     # Stored in DB
          ['Good',        'Good'],
          ['Fair',        'Fair'],
          ['Poor',        'Poor']
        ]
        
      GENDER_TYPES = [
          # Displayed     # Stored in DB
          ['Male',        'Male'],
          ['Female',      'Female']
        ]
    
    end
    Here is the form view:
    Code:
    <%= error_messages_for :patient %>
    <h1>Enter Patient Information</h1>
    <div id="patient-form-right">
    <% form_for :patient, :url => {:action => :add_patient_info} do |f| %>
    <fieldset><legend>Patient Information</legend>
    <p><label for="first_name">First Name *</label><br/>
    	<%= f.text_field :first_name, :size => 30, :maxlength => 60 %>
    </p>
    <p><label for="last_name">Last Name *</label><br/>
    	<%= f.text_field :last_name, :size => 30, :maxlength => 60 %>
    </p>
    <p><label for="address1">Address</label><br/>
    	<%= f.text_field :address1, :size => 30, :maxlength => 100 %><br/>
    	<%= f.text_field :address2, :size => 30, :maxlength => 100 %>
    </p>
    <p><label for="city">City / State / Zip Code</label><br/>
    	<%= f.text_field :city, :size => 20, :maxlength => 60 %> 
    	<%= f.text_field :state, :size => 2, :maxlength => 2 %> 
    	<%= f.text_field :zip_code, :size => 5, :maxlength => 5 %>
    </p>
    <p><label for="gender">Gender *</label><br/>
    	<%= f.select :gender, Patient::GENDER_TYPES, :prompt => 'Select a gender' %>
    </p>
    <p><label for="date_of_birth">Date of Birth *</label><br/>
    	<%= f.text_field :date_of_birth, :size => 10, :maxlength => 10 %> (mm/dd/yyyy)
    </p>
    <p><label for="height">Height *</label><br/>
    	<%= f.text_field :height, :size => 3, :maxlength => 3 %> inches
    </p>
    <p><label for="weight">Weight *</label><br/>
    	<%= f.text_field :weight, :size => 4, :maxlength => 4 %> lbs
    </p>
    <p><label for="prognosis">Prognosis *</label><br/>
    	<%= f.select :prognosis, Patient::PROGNOSIS_TYPES, :prompt => 'Select a prognosis' %>
    </p>
    </fieldset>
    </div>
    <p><%= submit_tag 'Next Step', :class => 'submit' %></p>
    <% end %>
    </div>

    This is my migration file:
    Code:
    class CreatePatients < ActiveRecord::Migration
      def self.up
        create_table :patients do |t|
          t.column :first_name,     :string, :limit => 60, :null => false
          t.column :last_name,      :string, :limit => 60, :null => false
          t.column :gender,         :string, :limit => 10, :null => false
          t.column :address1,       :string, :limit => 100, :null => true
          t.column :address2,       :string, :limit => 100, :null => true
          t.column :city,           :string, :limit => 100, :null => true
          t.column :state,          :string, :limit => 2, :null => true
          t.column :zip_code,       :string, :limit => 5, :null => true
          t.column :date_of_birth,  :date, :null => false
          t.column :height,         :integer, :null => false
          t.column :weight,         :integer, :null => false
          t.column :prognosis,      :string, :limit => 32, :null => false
          t.column :status,         :string, :limit => 32, :null => false, :default => 'NOT ACTIVE'
        end
      end
    
      def self.down
        drop_table :patients
      end
    end

  4. #4
    SitePoint Addict
    Join Date
    Jan 2006
    Posts
    268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try removing
    Code:
    attr_accessor :first_name, :last_name, :gender, 
                    :address1, :address2, :city, :state, :zip_code, 
                    :date_of_birth, :height, :weight,
                    :prognosis, :status
    from your model. ActiveRecord adds its own version and it might be messing with active record's implementation of them..
    If you give someone a program,
    you will frustrate them for a day;
    if you teach them how to program,
    you will frustrate them for a lifetime.

  5. #5
    SitePoint Member
    Join Date
    Aug 2006
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That seemed to work! I thought I had tried removing those before... Thanks!

  6. #6
    SitePoint Addict
    Join Date
    Jan 2006
    Posts
    268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sure thing. I think I ran into the same problem when I first started on rails.
    The attr_accessor method is still pretty handy if you've got a field that you want filled in on creation but don't have a field in the database for (like password_confirmation).
    It is also handy if you're not extending ActiveRecord.
    If you give someone a program,
    you will frustrate them for a day;
    if you teach them how to program,
    you will frustrate them for a lifetime.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •