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

  • Home
  • SEARCH
  • 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 7612361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:53:28+00:00 2026-05-31T01:53:28+00:00

There is probably a reeeeeally obvious answer to this, but I can’t find it,

  • 0

There is probably a reeeeeally obvious answer to this, but I can’t find it, so:

I have a timesheets model that has many time_entries, added via a nested form:

timesheets edit.html.erb:

<%= form_for @timesheet do |f| %>
.
.
.
    <%= f.fields_for :time_entries do |builder| %>
            <%= render 'time_entry_fields', :f => builder %>
    <% end %>

time_entry_fields partial

<%= f.text_field :project %>
<%= f.text_field :day1_hours %>
<%= f.text_field :day2_hours %>
<%= f.text_field :day3_hours %>
<%= f.text_field :day4_hours %>
<%= f.text_field :day5_hours %>
<%= f.text_field :day6_hours %>
<%= f.text_field :day7_hours %>

I’d like to define a method that shows the sum of all the entered hours – day1_hours + day2_hours + ... etc. I tried putting this in the model:

time_entry.rb

def total_hours
    d1 = day1_hours || 0
    d2 = day2_hours || 0
    d3 = day3_hours || 0
    d4 = day4_hours || 0
    d5 = day5_hours || 0
    d6 = day6_hours || 0
    d7 = day7_hours || 0
    d1 + d2 + d3 + d4 + d5 + d6 + d7
end   

And it works just fine if I call it on a TimeEntry object in the console:

1.9.3p0 :003 > TimeEntry.find(3).total_hours
TimeEntry Load (0.3ms)  SELECT "time_entries".* FROM "time_entries" WHERE "time_entries"."id" = $1 LIMIT 1  [["id", 3]]
=> 8 

But I can’t figure out how to call it from within the time_entry_fields partial in the form. I would normally be able to define @time_entry in the controller so that I could use @time_entry.total_hours, but how do you do this for an object in a nested form?

  • 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-31T01:53:29+00:00Added an answer on May 31, 2026 at 1:53 am

    f.object.total_hours in your partial should be what you want

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

Sidebar

Related Questions

There is probably a simple answer for this, but I couldn't find one, so
There is probably a simple answer to this that someone can help me with
There is probably already an answer for this somewhere, but I must not be
There probably is a fairly simple and straight-forward answer for this, but for some
There is probably an easy way to do this but I can't seem to
First of all there is probably a question like this already but i couldn't
I know that probably there is no concept of scope for macros, but please
I'm aware that there probably isn't a perfect solution to my question (this sounds
I'm painfully aware there probably isn't a magic bullet to this, but it's becoming
I know there probably is, but here's the situation: I have a mega menu

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.