0

Using Rails 4 I have a form that submits data to a Booking model. However, the form also submits an email address and this is intended for the Guest model.

I am ok inserting data into each table on submission of the form but my problem arises when I want to validate the data.

I haven't got a clue where to begin when trying to handle this extra parameter. Should I validate the email address using the Booking model (and if so how?) or should I somehow point the email part of the form to the Guest model and validate is separately?

I really am not sure how to approach this issue and any advice would be much appreciated.

tommyd456
  • 10,443
  • 26
  • 89
  • 163

1 Answers1

2

Use validates_associated?

Have something like:

class Booking
  has_many :guests

  validates_associated :guests
end

class Guest
  validates :name, :presence => true
end
Joe Pym
  • 1,826
  • 12
  • 23
  • I added this validates_associated :guest, :message => "Please enter your email address - but the error message displays with the word "Guest" at the beginning. – tommyd456 Sep 12 '13 at 20:24
  • Try looking here @tommyd456 http://stackoverflow.com/questions/808547/fully-custom-validation-error-message-with-rails – Joe Pym Sep 13 '13 at 14:12