ruby-on-rails - 如何自动更新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
解决方案
我相信在您的情况下,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
推荐阅读
- excel - VBA将工作表作为参数传递给宏
- javascript - 在使用部分检查用户 ID 时出现不一致的错误。(不和谐.js)
- python - 如何修复错误:(psycopg2.errors.NotNullViolation)列“id”中的空值违反非空约束?
- java - Fabric API 获取草块的颜色
- kubernetes - gke 入口无法将流量路由到服务
- react-native - react native改变状态后如何更新组件样式
- python-3.x - 有没有办法在 py2app 应用程序中使用 ffmpeg 二进制/unix 可执行文件在没有安装它的计算机上运行 ffmpeg?
- javascript - jQuery 显示/隐藏文本按钮 onclik 或平滑隐藏
- google-apps-script - 如何将未经评估的文本数据写入谷歌表格单元格
- fmod - FMOD 获取参数 OK,设置参数 ERR_INVALID_PARAM