首页 > 解决方案 > 在另一个表中创建一个新行,并在另一个表中更改状态

问题描述

我有一个请求表,其中状态为字段,还有一个遇到表。状态字段显示状态表的下拉列表。我想要的是,随着状态的每次变化,都会在遇到表中创建一个新记录,该记录保存每个请求的所有状态的记录。我需要关于如何实现这一点的建议。

申请表

<%= 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

标签: ruby-on-railsruby-on-rails-5

解决方案


在 request.rb 上,你有一个 ? 标记在末尾self.changes.keys.include?(:status_id)?,这是一个错误。让它喜欢self.changes.keys.include?(:status_id)


推荐阅读