class RealisationImage < ActiveRecord::Base
  belongs_to :image_file
  
  belongs_to :realisation
  
  
  
  after_save do
    realisation_default_image
  end
  
  after_destroy do
    realisation_default_image
  end
  
  def realisation_default_image
    if !self.realisation.default_image
      self.realisation.default_image = self.realisation.realisation_images.order("position").first
      self.realisation.save
    else
      self.realisation.default_image = nil
      self.realisation.save
    end
    
  end
end
