Topic: mutliple model validation

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 =
    3.times { }

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

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

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

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

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

Additionally, if anybody would know how to make the "#@contactinfo.dtelstupd =" 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 to work, try this:

    params[:contactinfos].each_value do |contactinfo|
      unless contactinfo.values.all?(&:blank?)
        contactinfo[:dtelstupd] =

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

# in Contactinfo model
def before_create
  self.dtelstupd =

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!