When a user creates a new booking record, part of the data entered is an email address. This email address is used to create a new guest record at the same time if they don't already exist. The booking should have the guest id as part of the record.
In my models I have defined the relationships so:
accommodations has_many bookings
guests has_many bookings
bookings belongs_to accommodations
bookings belongs_to guests
This is what I have so far in the create action of my BookingsController:
...
def create
accommodation = current_user.accommodation
@booking = accommodation.bookings.build(post_params)
@guest = accommodation.guests.build(params[:email])
if @booking.save
flash[:success] = 'The booking has been added successfully.'
redirect_to :controller => 'bookings', :action => 'index'
else
render 'new'
end
end
...
My questions are:
- Should I use 'build' twice as I want the new booking to have the guest id?
- How can I check if guest exists already using email?
- Is it safe/secure to use params[:email] when building the guest?