I'm wondering if there is any way we can optimize the following code.
Scenario is Library
's active
field should be true
if it has any available
book.
class Book < ApplicationRecord
belongs_to :library
scope :available, -> { where.not(deleted: true) }
after_commit :update_library_active, on: [:update, :create]
def update_library_active
self.library.update_active_status
end
end
class Library < ApplicationRecord
has_many :books
def update_active_status
self.update(active: books.available.present?)
end
end
active
attribute based on the associated books. \$\endgroup\$