Я пытаюсь создать запись о столкновении из модели запроса.Я хочу, чтобы after_update: create_encounter работал, только если в записи запроса есть какие-либо изменения, для которых я пытался создать before_update: check_changes, но я не могу понять, как реализовать функцию check_changes, чтобы увидеть, есть ли какие-либоизменения в записи запроса.Пожалуйста, помогите
record.rb
class Request < ApplicationRecord
after_create :create_encounter
before_update :check_changes
after_update :create_encounter
has_many :encounters, dependent: :destroy
def create_encounter
hello = Encounter.new
hello.request_id = self.id
hello.status_change_date = DateTime.now.to_date
hello.notes = self.notes
hello.save
end
def check_changes
end
end
схема для запроса
create_table "requests", force: :cascade do |t|
t.string "applicant_name"
t.string "pickup_location"
t.string "notes"
end
Добавление
def create_encounter
if self.changed?
hello = Encounter.new
hello.request_id = self.id
hello.status_change_date = DateTime.now.to_date
hello.notes = self.notes
hello.save
end
end