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 6249363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:13:32+00:00 2026-05-24T13:13:32+00:00

I’m working on my first Rails Application. I am a little stuck with the

  • 0

I’m working on my first Rails Application. I am a little stuck with the time. I’m working on a recipe application. I need to add two fields.

  • Preparation Time
  • Cook Time

Out of the two, i would like to add the two fields to come up with the Total Time needed to prepare the meal.

I approached it the wrong way which doesn’t have logic :(. Basically i have two fields and i used f.select to select predefined times. But the problem i have with that approach is that when adding the two, it ignores the Gregorian format e.g 40 minutes + 50 mins will become 90 Min instead of 1hour 30.

I would appreciate any help from the community.

  • 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-05-24T13:13:33+00:00Added an answer on May 24, 2026 at 1:13 pm

    A quick example:

    prep_time = 40.minutes
    cook_time = 50.minutes
    
    total_time = prep_time + cook_time
    formatted_total_time = Time.at(total_time).gmtime.strftime('%I:%M')
    
    # outputs 01:30 which is HOURS:MINUTES format
    

    If you wanted 90 minutes instead:

    formatted_total_time = total_time / 60
    
    # outputs 90
    

    Update:

    Put this in the helper file associated with whatever view you are using this in (i.e. app/helpers/recipes_helper.rb)

    module RecipesHelper
    
      def convert_to_gregorian_time(prep_time, cook_time)
        # returns as 90 mins instead of 1hr30mins
        return (prep_time + cook_time) / 60
      end
    
    end
    

    Then you’d just call it in your view (i.e. app/views/recipes/show.html.haml like:

    # Note: this is HAML code... but ERB should be similar
    
    %p.cooking_time
      = convert_to_gregorian_time(@recipe.prep_time, @recipe.cook_time)
    

    If you are storing the times in the database as integers (which you SHOULD be doing), then you can do this:

    %p.cooking_time
      = convert_to_gregorian_time(@recipe.prep_time.minutes, @recipe.cook_time.minutes)
    

    where @recipe.prep_time is an integer with a value of 40 and @recipe.cook_time is an integer with a value of 50

    and your database schema would look something like:

    # == Schema Information
    #
    # Table name: recipes
    #
    #  id                 :integer         not null, primary key
    #  prep_time          :integer
    #  cook_time          :integer
    #  # other fields in the model...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I've tracked down a weird MySQL problem to the two different ways I was
I need a function that will clean a strings' special characters. I do NOT
I have thousands of HTML files to process using Groovy/Java and I need to
I'm parsing an XML file, the creators of it stuck in a bunch social
I'm making a simple page using Google Maps API 3. My first. One marker

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.