Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8800541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:37:01+00:00 2026-06-14T00:37:01+00:00

In my Rails 3.2 app, I want to populate some fields based on calculations

  • 0

In my Rails 3.2 app, I want to populate some fields based on calculations where the field values users enter are the variables. However, with my current code, the calculation seems to only work based on the values already in the database – it doesn’t calculate correctly on the initial save, but it will calculate correctly if I go back in the record and save it a second time.

I have these four fields in my model (Trade):

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

The user creates a trade with an entry price, and then later edits the trade with the exit_price. When the exit_price is entered, the app should calculate percent_result and dollar_result. However, right now, these result fields are not populating correctly on the first update – it seems to be because it doesn’t read the exit_price from the field (when a user enters it in the form), only once it is saved in the DB.

What is going wrong in my controller?

my controller:

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

the view

<%= 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 %>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T00:37:03+00:00Added an answer on June 14, 2026 at 12:37 am

    This would probably be better accomplished with a before_save filter in your model.

    Add

    before_save :calculate_results
    

    to the top of your model and then define

    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
    

    in your model as well. Taking this approach ensures that your results will always be consistent with your values for entry and exit price. Enforcing this in the controller violates the Rails principle of “thick model and thin controller” and may also lead to data consistency issues.

    An even more consistent way of doing this would be to define dollar_result and percent_result as methods in your model. As your model is now, you have dollar_result stored in the database even though it is a derived value. As a general rule, you should only have one representation of each piece of data whereas here you have two. A helper method might look something like

    def dollar_result
        self.exit_price - self.entry_price unless self.exit_price.blank? || self.entry_price.blank?
    end
    

    You would define a similar method for percent_result. Using this method, you can guarantee that all of your data is consistent because it only has one, canonical representation in the system.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to populate some data to use in my Rails app. However I
I have a simple_form in my rails app that I want to pre-populate some
In my Rails app I want users to be able to input video links
I have a Rails app (running 3.2.2) and I want to cache my users'
I want my rails app to accept dates for a date field in the
In a Rails app, I want to allow users to send messages from one
I want my Rails app to call a Java program to do some labor-intensive
I have a Rails app (Postgres) and I want to add validation based on
I have a rails app and I want to deploy it on a server
I am writing a Ruby on Rails app and I want a method to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.