diff --git a/app/models/galleries_icon.rb b/app/models/galleries_icon.rb index dd22ceea09..ad4be7c212 100644 --- a/app/models/galleries_icon.rb +++ b/app/models/galleries_icon.rb @@ -3,8 +3,13 @@ class GalleriesIcon < ActiveRecord::Base belongs_to :gallery accepts_nested_attributes_for :icon, allow_destroy: true + after_create :set_has_gallery after_destroy :unset_has_gallery + def set_has_gallery + icon.update_attributes(has_gallery: true) + end + def unset_has_gallery return if icon.galleries.present? icon.update_attributes(has_gallery: false) diff --git a/app/models/gallery.rb b/app/models/gallery.rb index f975c5c478..2303ce4c5f 100644 --- a/app/models/gallery.rb +++ b/app/models/gallery.rb @@ -5,7 +5,7 @@ class Gallery < ActiveRecord::Base has_many :galleries_icons accepts_nested_attributes_for :galleries_icons, allow_destroy: true - has_and_belongs_to_many :icons, -> { order('LOWER(keyword)') }, after_add: :set_has_gallery, after_remove: :unset_has_gallery + has_many :icons, -> { order('LOWER(keyword)') }, through: :galleries_icons has_many :characters_galleries has_many :characters, through: :characters_galleries @@ -21,16 +21,4 @@ def default_icon def character_gallery_for(character) characters_galleries.where(character_id: character).first end - - private - - def set_has_gallery(icon) - return unless valid? # don't change icon status unless the gallery being saved is valid - icon.update_attributes(has_gallery: true) - end - - def unset_has_gallery(icon) - return if icon.galleries.present? - icon.update_attributes(has_gallery: false) - end end