Обновление атрибута не работает

У меня есть две колонки:

background_image_id (which is a relation)
background_image (which is a copy of that image, with various versions)

В моем before_update метод, который я должен скопировать файл Carrierwave из Medium.find(background_image_id) в background_image колонка.

И я не могу этого сделать. NIL сохраняется в базе данных. Понятия не имею почему.


контроллер:

def update
  if params[:profile].blank? || params[:profile][:background_image_id].blank?
    flash[:alert] = 'Please select image'
    redirect_to edit_candidate_background_image_path
  else
    resource.background_image_id = params[:profile][:background_image_id]

    if resource.save
      flash[:notice] = 'Profile was updated'
     else
      flash[:alert] = "Can't update profile"
    end

    redirect_to edit_candidate_background_image_path
  end
end

Модель:

before_update :copy_background_image, if: :background_image_id_changed?  
...

def copy_background_image
  medium = Medium.find(background_image_id)
   if medium.present?
    self.background_image = medium.image.file
    puts self.inspect # outputs a model where background_image is nil
    puts self.background_image.inspect # outputs an uploader with correct image PRESENT
  end
end

0 ответов

Другие вопросы по тегам