如何自动更新Rails中的字段

时间:2018-06-27 17:45:48

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

我有一个常规设置表,父母表,孩子表和资金表。子表具有funding_id。每个孩子可以有多个资助申请。在资金表中有一个只能由管理员更改的字段状态。一旦管理员批准了该申请,我希望从子表中的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 %>

funding.rb

class Funding < ApplicationRecord
belongs_to :child
end

child.rb

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

1 个答案:

答案 0 :(得分:0)

我相信在您的情况下,AR before_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