Wrap text
Report abuse
def remove_existing_customers!(customer_attribs, tour_id = nil)
existing_customers = []
customer_attribs.reject! do |key, value|
existing_customer = nil
tour = Tour.find(tour_id) if tour_id
if value['id'].nil? || (tour && tour.customers.detect { |customer| customer.id.to_s == value['id'].to_s }.nil?)
if existing_customer = Customer.find_by_name(value['name'])
existing_customers << existing_customer
end
end
existing_customer
end unless customer_attribs.nil?
existing_customers
end
def create
existing_customers = remove_existing_customers!(params[:tour][:customers_attributes])
@tour = Tour.new(params[:tour])
respond_to do |format|
if @tour.save
@tour.customers << existing_customers
@tour.save
flash[:notice] = 'Tour was successfully created.'
format.html { redirect_to(@tour) }
format.xml { render :xml => @tour, :status => :created, :location => @tour }
else
format.html { render :action => "new" }
format.xml { render :xml => @tour.errors, :status => :unprocessable_entity }
end
end
end