ruby-on-rails - 在另一个表中创建一个新行,并在另一个表中更改状态
问题描述
我有一个请求表,其中状态为字段,还有一个遇到表。状态字段显示状态表的下拉列表。我想要的是,随着状态的每次变化,都会在遇到表中创建一个新记录,该记录保存每个请求的所有状态的记录。我需要关于如何实现这一点的建议。
申请表
<%= form_for([@client, @request], local: true) do |form| %>
<div class="field">
<%= form.label :status_id %>
<%= form.collection_select :status_id, Status.all, :status, :status, {prompt: "Select"}, autofocus:true %>
</div>
<% end %>
遇到表模式
create_table "encounters", force: :cascade do |t|
t.integer "request_id"
t.date "status_change_date"
t.integer "admin_id"
t.string "notes"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
请求.rb
has_many :encounters
遇到.rb
belongs_to :request
请求.rb
after_update :create_encounter
def create_encounter
if self.changes.keys.include?(:status_id)?
puts "updated"
end
end
解决方案
在 request.rb 上,你有一个 ? 标记在末尾self.changes.keys.include?(:status_id)?
,这是一个错误。让它喜欢self.changes.keys.include?(:status_id)
。
推荐阅读
- r - 如何为绘图中的一行设置 scale_x_date
- esp8266 - ESP 32 和 8266 的共享代码库?
- asp.net-core - 当断言函数返回预期的 DTO 对象时,如何在 DTO 对象类的 getter 上获得 100% 的测试覆盖率?
- node.js - 让节点 REPL 在文件更改时重新加载导入的模块?
- mysql - 如何在数据库左窗格中恢复 PHPMyAdmin 函数视图
- c# - 无法使用 C# auth 从某些套接字中找到只读域控制器
- linux - 关于用于构建和运行的 Boot2docker 设置的一些问题
- firebase-realtime-database - 错误:身份验证选项与现有会话令牌不匹配,也不是交互式会话,因此无法手动重新身份验证
- java - 监听集合中的多个文档并更新recyclerview
- c# - 如何获取与列表视图数据模板中存在的复选框关联的值?