Topic: mutliple model validation

Hello,
I have two models that are being shown in the same view.  These are setup in the same fashion as Ryanb's tutorial on multiple models.  Everything thing is working fine (viewing the page and writing to the dB), until I try to put validation into the second model (contactinfo) at which point it blows up.

Here is the controller create method that is used (personalinfo_controller.rb)

  def new
    @personalinfo = Personalinfo.new
    3.times { @personalinfo.contactinfos.build }
  end

  def create
    @personalinfo = Personalinfo.new(params[:personalinfo])
    @personalinfo.active = '1'
    @personalinfo.dtelstupd = Date.today
    params[:contactinfos].each_value do |contactinfo|   
      @personalinfo.contactinfos.build(contactinfo) unless contactinfo.values.all?(&:blank?) 
      #@contactinfo.dtelstupd = Date.today
    end
    if @personalinfo.save
      #flash[:notice] = 'Personalinfo was successfully created.'
      redirect_to :controller => 'affactions', :action => 'new'
    else
      render :action => 'new'
    end
  end

  def edit
    @personalinfo = Personalinfo.find(params[:id])
  end


and here is the problematic model (contactinfo.rb
class Contactinfo < ActiveRecord::Base
    belongs_to :personalinfo
   
    validates_presence_of :name, :yearsknown, :phone
end

and the model with working validation (personalinfo.rb)
class Personalinfo < ActiveRecord::Base
  has_many :contactinfos 
  has_many :educationinfos
  has_many :employmentinfos
  has_many :offenses

  has_one :affaction
  has_one :appconsidersumm
  has_one :policyack
  has_one :positioninfo
  has_one :profinfo
 
  validates_presence_of :address1, :city, :state, :postal, :fname, :lname
end


The error message that is reported is "Contactinfos is invalid"

Additionally, if anybody would know how to make the "#@contactinfo.dtelstupd = Date.today" work properly, I would appreciate that as well.
Thank you!

Re: mutliple model validation

Are you certain each attribute (name, yearsknown, and phone) is getting set in every contactinfo model? If any of the model's attributes are blank than this will cause the error. Take a look at the development.log file to make sure the data for each attribute is being passed.

As for getting the Date.today to work, try this:

    params[:contactinfos].each_value do |contactinfo|
      unless contactinfo.values.all?(&:blank?)
        contactinfo[:dtelstupd] = Date.today
        @personalinfo.contactinfos.build(contactinfo)
      end
    end

You may want to put this in a before_create callback instead:

# in Contactinfo model
def before_create
  self.dtelstupd = Date.today
end

Railscasts - Free Ruby on Rails Screencasts

Re: mutliple model validation

Thank you Ryanb,
Upon looking into the development.log, I realized I was calling the wrong table.  One is named contactinfo and another is called contactdetail.  It usually is a good thing to call the right table.  Sorry about that.  Thank you for your help with this and the date portion!