基于计算填充Rails字段

yel*_*ign 2 ruby-on-rails-3

在我的Rails 3.2应用程序中,我想基于计算填充一些字段,其中用户输入的字段值是变量.但是,使用我当前的代码,计算似乎只能根据数据库中已有的值工作 - 它在初始保存时无法正确计算,但如果我返回记录并保存一秒,它将正确计算时间.

我的模型中有这四个字段(Trade):

  1. entry_price
  2. exit_price
  3. percent_result
  4. dollar_result

用户创建具有入场价格的交易,然后使用exit_price编辑交易.输入exit_price后,应用程序应计算percent_result和dollar_result.但是,现在,这些结果字段在第一次更新时没有正确填充 - 这似乎是因为它没有从字段中读取exit_price(当用户在表单中输入时),只有在将其保存在D B.

我的控制器出了什么问题?

我的控制器:

def update
  @trade = Trade.find(params[:id])
  exit_price = params[:trade][:exit_price]

  if !exit_price.blank?
    @trade.percent_result = ((exit_price.to_f - @trade.entry_price)/@trade.entry_price) * 100
    @trade.dollar_result = exit_price.to_f - @trade.entry_price 
  end

  params[:trade][:exit_date] = Date.strptime(params[:trade][:exit_date], '%m/%d/%Y') unless params[:trade][:exit_date].blank?
  params[:trade][:entry_date] = Date.strptime(params[:trade][:entry_date], '%m/%d/%Y') unless params[:trade][:entry_date].blank?
  respond_to do |format|
    if @trade.update_attributes(params[:trade])
      format.html { redirect_to @trade, :flash => {:share =>"Your trade was successfully updated.  Don't forget to share it with your friends, so you can profit together!"} }
      format.json { head :no_content }
    else
      format.html { render action: "edit" }
      format.json { render json: @trade.errors, status: :unprocessable_entity }
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

风景

<%= simple_form_for(@trade, :html=>{:class=> "form-horizontal well"})  do |f| %>  
  <%= f.text_field :entry_price, :class=>"input-small" %>
  <%= f.text_field :exit_price, :class=>"input-small" %>

  <%= submit_tag "Edit Trade" %>
<% end %>
Run Code Online (Sandbox Code Playgroud)

小智 6

使用模型中的before_save过滤器可能会更好.

before_save :calculate_results
Run Code Online (Sandbox Code Playgroud)

到模型的顶部然后定义

def calculate_results
    unless self.exit_price.blank? || self.entry_price.blank?
        self.percent_result = ((self.exit_price - self.entry_price)/self.entry_price) * 100
        self.dollar_result = self.exit_price - self.entry_price 
    end
end
Run Code Online (Sandbox Code Playgroud)

在你的模型中也是如此.采用这种方法可确保您的结果始终与进入和退出价格的值保持一致.在控制器中执行此操作违反了"厚模型和精简控制器"的Rails原则,并且还可能导致数据一致性问题.

更加一致的方法是将dollar_result和percent_result定义为模型中的方法.正如您的模型现在,即使它是派生值,您也会将dollar_result存储在数据库中.作为一般规则,您应该只对每条数据有一个表示,而在这里您有两个.辅助方法可能看起来像

def dollar_result
    self.exit_price - self.entry_price unless self.exit_price.blank? || self.entry_price.blank?
end
Run Code Online (Sandbox Code Playgroud)

您可以为percent_result定义类似的方法.使用此方法,您可以保证所有数据都是一致的,因为它在系统中只有一个规范表示.