我正在尝试根据请求模型创建一个遭遇记录。我想要的是after_update:create_encounter仅在我尝试为其创建before_update:check_changes的请求记录中有任何更改时才起作用,但是我无法弄清楚如何实现check_changes函数以查看是否存在任何更改请求记录中的更改。请帮助
record.rb
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
添加
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background-color: yellow;
}
.container {
background-color: blue;
width: 95vw;
margin: 0 2.5vw;
height: 50px;
}
.top-nav {
background: green;
opacity: 0.9;
position: fixed;
top: 0;
left: 2.5vw;
width: 95vw;
height: 30px;
}
答案 0 :(得分:1)
您可以使用ActiveRecord::AttributeMethods::Dirty
中的saved_changes?()
,它会告诉您对save
的最后一次调用是否包含任何更改。
答案 1 :(得分:1)
您必须在保存之前致电:create_encounter。
答案 2 :(得分:0)
class Request < ApplicationRecord
before_save :create_encounter
after_create :create_encounters
belongs_to :clinic
belongs_to :client
has_many :encounters, dependent: :destroy
def create_encounter
if self.changed?()
hello = Encounter.new
hello.request_id = self.id
hello.admin_id = current_admin_id
hello.status_change_date = DateTime.now.to_date
hello.notes = self.notes
hello.save
end
end
def create_encounters
hello = Encounter.new
hello.request_id = self.id
hello.admin_id = current_admin_id
hello.status_change_date = DateTime.now.to_date
hello.notes = self.notes
hello.save
end
def current_admin_id
Admin.current_admin.try(:id)
end
end