title: How can I return validation errors for objects created after_save?
body: <p>I'm writing a booking system which handles recurring bookings using the <a href="https://github.com/seejohnrun/ice_cube" rel="nofollow">ice_cube</a> gem. A <code>Booking</code> has_many <code>BookingItem</code>s, one for each occurrence in the recurrence rule, and these are created in a method which is called by <code>Booking</code>'s after_save callback.</p>
  
  <p>This was all working fine until I added a validation to <code>BookingItem</code> which avoids double booking by checking that there isn't already a <code>BookingItem</code> at the given time. This validation raises an error which I'd like to display on the booking form but at the moment it just silently prevents the <code>Booking</code> from being saved - since the error is raised by the <code>BookingItem</code> it's not being passed back to the form for the <code>Booking</code>.</p>
  
  <p>app/models/booking.rb</p>
  
  <pre><code>class Booking &lt; ActiveRecord::Base
    include IceCube
  
    has_many :booking_items, :dependent =&gt; :destroy
  
    after_save :recreate_booking_items!
  
    # snip
  
    private
  
    def recreate_booking_items!
      schedule.all_occurrences.each do |date|
        booking_items.create!(space: self.requested_space, 
                              booking_date: date.to_date,
                              start_time: Time.parse("#{date.to_date.to_default_s} #{self.start_time.strftime('%H:%M:00')}"),
                              end_time: Time.parse("#{date.to_date.to_default_s} #{self.end_time.strftime('%H:%M:00')}"))
      end
    end
  end
  </code></pre>
  
  <p>app/models/booking_item.rb</p>
  
  <pre><code>class BookingItem &lt; ActiveRecord::Base
    belongs_to :booking
  
    validate :availability_of_space
  
    # snip
  
    private
  
      def availability_of_space
        unless space.available_between? DateTime.parse("#{booking_date}##{start_time}"), DateTime.parse("#{booking_date}##{end_time}")
          errors[:base] &lt;&lt; "The selected space is not available between those times."
        end
      end
  end
  </code></pre>
  
  <p>app/views/booking/_form.html.erb</p>
  
  <pre><code>&lt;% if @booking.errors.any? %&gt;
    &lt;div id="error_explanation"&gt;
      &lt;p&gt;&lt;%= pluralize(@booking.errors.count, "error") %&gt; prohibited this booking from being saved:&lt;/p&gt;
      &lt;ul&gt;
        &lt;% @booking.errors.full_messages.each do |msg| %&gt;
          &lt;li&gt;&lt;%= msg %&gt;&lt;/li&gt;
        &lt;% end %&gt;
      &lt;/ul&gt;
    &lt;/div&gt;
  &lt;% end %&gt;
  
  &lt;%= form_for(@booking, :html =&gt; { :class =&gt; "nice custom"}) do |f| %&gt;
    ...
  &lt;% end %&gt;
  </code></pre>