首页 > 解决方案 > 如何自动更新rails中的字段

问题描述

我有一个常规设置表、父母表、儿童表和资金表。Children 表有funding_id。每个孩子可以有多个资助申请。在资金表中有一个只能由管理员更改的字段状态。一旦管理员批准了申请,我希望从 children 表中的 amount_remaining 字段中减去请求的金额。默认的 amount_remaining 是常规设置中的 amount_current_year。我怎样才能做到这一点。请帮助我。

资金表架构

 create_table "fundings", force: :cascade do |t|
    t.string "type_of_activity"
    t.string "season"
    t.text "activity_details"
    t.string "name_of_organisation"
    t.date "activity_start_date"
    t.string "number_of_weeks"
    t.string "days_per_week"
    t.string "hours_per_day"
    t.integer "program_registration_cost"
    t.string "family_contribution"
    t.integer "other_funds"
    t.string "other_fund_provider"
    t.integer "amount_requested"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "child_id"
    t.string "status"
    t.date "date_submitted"
  end

一般设置方案

  create_table "generalsettings", force: :cascade do |t|
    t.integer "amount_current_year"
    t.integer "amount_next_year"
    t.date "current_year"
    t.date "next_year"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

子表架构

create_table "children", force: :cascade do |t|
    t.string "firstname"
    t.string "lastname"
    t.date "dateofbirth"
    t.string "gender"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "parent_id"
    t.integer "amount_remaining"
  end

_form.html.erb(资助申请表)

<div class='row'>
  <div class= 'col-xs-12'>
    <%= form_for([@parent, @child, @funding], :html => {class: "form-horizontal",role: "form"}) do |form| %>
      <div class = "form-group">
        <div class="control-label col-sm-2">
          <%= form.label :type_of_activity, class: "required" %>
        </div>
        <div class="col-sm-8">
          <%= form.select :type_of_activity, ['Swimming', 'Soccer', 'Cricket', 'Basket Ball'], required: true %>
        </div>
      </div>
      <div class = "form-group">
        <div class="control-label col-sm-2">
          <%= form.label :activity_details, class: "required" %>
        </div>
        <div class="col-sm-8">
          <%= form.text_area :activity_details, required: true %>
        </div>
      </div>
      <div class = "form-group">
        <div class="control-label col-sm-2">
          <%= form.label :name_of_organisation, class: "required" %>
        </div>
        <div class="col-sm-8">
          <%= form.select :name_of_organisation, ['BCCI', 'IPL', 'Sahara', 'Not listed'], class: "form-control", autofocus:true, required: true %>
        </div>
      </div>
      <div class = "form-group">
        <div class="control-label col-sm-2">
          <%= form.label :activity_start_date, class: "required" %>
        </div>
        <div class="col-sm-8">
          <%= form.date_field :activity_start_date, min: Date.today, required: true %>
        </div>
      </div>
      <div class = "form-group">
        <div class="control-label col-sm-2">
          <%= form.label :program_registration_cost, class: "required" %>
        </div>
        <div class="col-sm-8">
          <%= form.text_field :program_registration_cost, required: true %>
        </div>
      </div>
      <div class = "form-group">
        <div class="control-label col-sm-2">
          <%= form.label :amount_requested, class: "required" %>
        </div>
        <div class="col-sm-8">
          <%= form.text_field :amount_requested, required: true %>
        </div>
      </div> 
      <div class = "form-group">
        <div class="control-label col-sm-2">
          <%= form.label :date_submitted, class: "required" %>
        </div>
        <div class="col-sm-8">
          <%= form.date_field :date_submitted, value: Date.today, readonly:true, required: true %>
        </div>
      </div>
      <div class = "form-group">
        <div class="control-label col-sm-2">
          <%= form.label :status %>
        </div>

      <% if current_user.admin? %>
        <div class="col-sm-8">
          <%= form.select :status,['Pending', 'Approved', 'Declined'], class: "form-control" %>
        </div>
      <% else %>
        <div class="col-sm-8">
          <%= form.select :status,['Pending', 'Approved', 'Declined'], {class: "form-control"}, {:disabled => true} %>
        </div>
      <% end %>

      </div> 
      <div class="form-group">
        <div class="col-sm-10">
          <%= form.submit "Apply", class: 'btn btn-primary btn-lg' %>
        </div>
      </div>
    </div>
  </div>

    <% end %>

资金.rb

class Funding < ApplicationRecord
belongs_to :child
end

子.rb

class Child < ApplicationRecord
    belongs_to :parent
    has_many :fundings, dependent: :destroy
end

标签: ruby-on-railsrubyruby-on-rails-3ruby-on-rails-4ruby-on-rails-5

解决方案


我相信在您的情况下,ARbefore_save回调可能会有所帮助。

class Funding < ApplicationRecord
  has_one :child
  before_save do
    if status_changed? && status == 'Approved'
      amount_remaining = child.amount_remaining - amount_requested
      child.update_attributes(amount_remaining: amount_remaining)
    end
  end
  ...
end

推荐阅读