class Cdao::LibrarySubject < Cdao::Base acts_as_list column: :rank, scope: %i[library_id subject_parent_id] belongs_to :library, optional: false delegate :name, :rank, to: :library, prefix: true belongs_to :subject, optional: false validates :library_id, uniqueness: { scope: %i[subject_id] } after_save do update_column :subject_parent_id, subject.parent_id # rubocop:disable Rails/SkipsModelValidations end def self.import Subject.find_each do |subject| subject.library_subjects.where(library_id: subject.library_id).first_or_create end end def self.import_rank_from_subjects Subject.find_each do |subject| subject.library_subjects.each do |subject_library| subject_library.update(rank: subject.rank) end end end end