# app/models/currency.rb
class Currency < ApplicationRecord
# validations
validates :iso_code, presence: true, uniqueness: { case_sensitive: false }
validates :rate, presence: true
# scopes
scope :base, -> { find_by(is_base: true) }
# associations
has_many :conversions, dependent: :destroy
end
# app/models/conversion.rb
class Conversion < ApplicationRecord
# associations
belongs_to :currency
belongs_to :to, class_name: "Currency"
# validations
# rubocop:disable Rails/UniqueValidationWithoutIndex
validates :to_id, uniqueness: { scope: :currency_id }
# rubocop:enable Rails/UniqueValidationWithoutIndex
validate :currency_equal_to_to
# callbacks
after_create_commit do
broadcast_prepend_to 'conversions'
end
after_update_commit do
broadcast_replace_to self
end
after_destroy_commit do
broadcast_remove_to 'conversions', target: "conversion_#{self.id}"
end
def currency_equal_to_to
errors.add(:to, "equal to from") if currency == to
end
end
Şu şekilde bir kullanımım varken, conversion’lerden herhangi birinin currency
veya to
ilişkisinde bir update olduğunda hotwire ile
# app/views/conversions/_conversion.slim
= turbo_frame_tag dom_id(conversion) do
.row
.col #{conversion.currency.iso_code}
.col #{conversion.to.iso_code}
.col #{conversion.to.rate}
partial’ını güncellemek istiyorum, nasıl yapabilirim bir fikrim yok maalesef, yardımcı olursanız cok sevinirim.