accepts_nested_attributes_for不会触发对孩子的回调?

时间:2015-10-29 12:17:11

标签: ruby-on-rails

我有一个名为' Shift'其中有许多儿童模特,每个都叫做“预订”。当我更新shift时,孩子们没有被更新(在开发环境中),尽管测试正在通过。

更详细信息:

在控制器中,我们运行此操作来处理评论。

def review
    @shift = Shift.find(params[:id])
    @shift.start_will_change! # this makes the 'shift' object 'dirty', triggering callbacks.
    if @shift.update(review_params)
      @shift.update(reviewed: true)
      redirect_to employers_shift_path @shift
    else
      flash[:notice] = "Could not leave review. If this problem persists please contact will@get-rota.com"
      redirect_to employers_shift_path @shift
    end
  end

以下是通过的测试。到目前为止一切都很好。

 test "accepts reviews that change the time" do
    assert_nil @booking.start_actual
    post :review, id: @shift.id, shift: {
      bookings_attributes: {
        "0"=> {
          "rating_of_worker"=>"4",
          "feedback_for_worker"=>"Great job",
          "edit_times"=>"true",
          "start_date"=> (@booking.start + 1.day).to_s, 
          "start_time_hours"=>(@booking.start - 1.hour).to_s, 
          "start_time_minutes"=>@booking.start.to_s, 
          "end_time_hours"=>@booking.ending.to_s, 
          "end_time_minutes"=>@booking.ending.to_s, 
          "id"=>@booking.id}
        }
      }

    @booking.reload

    assert_equal 4, @booking.rating_of_worker
    assert_equal "Great job", @booking.feedback_for_worker
    assert_not_nil @booking.start_actual
    assert_equal (@booking.start - 1.hour + 1.day), @booking.start_actual
    assert_not_nil @booking.ending_actual
    assert_equal (@booking.ending + 1.day), @booking.ending_actual


    assert_redirected_to employers_shift_path @shift
  end

其他信息:

在这里预订:

class Booking < ActiveRecord::Base

  belongs_to :shift

  after_validation :update_actual_start_and_end

  attr_accessor :start_time_hours,
                :start_time_minutes,
                :end_time_hours,
                :end_time_minutes,
                :edit_times,
                :start_date


  def update_actual_start_and_end
    if edit_times == 'true'
      date = DateTime.parse(start_date)
      self.start_actual = start.change({ year: date.year,
                            month: date.month,
                            day: date.day,
                            hour: DateTime.parse(start_time_hours).hour,
                            min: DateTime.parse(start_time_minutes).minute })
      self.ending_actual = ending.change({ hour: DateTime.parse(end_time_hours).hour,
                                           min: DateTime.parse(end_time_minutes).minute })
      if self.start_actual > self.ending_actual
        self.ending_actual += 1.day
      end
    end
  end


end

这里的转变:

class Shift < ActiveRecord::Base
  has_many :bookings, dependent: :destroy
  accepts_nested_attributes_for :bookings
end

0 个答案:

没有答案