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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:40:51+00:00 2026-05-27T15:40:51+00:00

I am sorry if I am asking the question poorly. I have a Rails

  • 0

I am sorry if I am asking the question poorly. I have a Rails 3.1 app with models (simplified) like so:

class Employee < ActiveRecord::Base
  has_many    :merged_children, :class_name => 'Employee', :foreign_key => "merge_parent_id"
  has_many    :timesheets

  def total_time 
    merged_children.timesheets.in_range(range).hours_minutes.sum 
  end
end

class Timesheet < ActiveRecord::Base
  belongs_to :employee

  def in_range(range)
    # filter records based on transaction_date in range 
  end

  def hours_minutes
    (hours + minutes/60.0).to_f
  end
end

Note: The in_range method acts as a scope, essentially, and hours_minutes is a calculation. hours_minutes is valid for each timesheet record in the resulting dataset, and then total_time should sum those values and return the amount.

The “total_time” method is not working because employee.merged_children returns an array and timesheets is meant to run against a single Employee object.

Is there any way to structure the “total_time” so that it still sends one query to the db? It seems inelegant to iterate over the merged_children array, issuing a query for each. Not sure if a direct call to an Arel table would help or hurt, but I am open to ideas.

If we get it right, the resulting SQL should effectively look something like:

SELECT sum(hours + minutes/60.0)
FROM employees e1 join employees e2 on e1.id = e2.merge_parent_id join timesheets t on t.employee_id = e2.id
WHERE e1.id = [@employee.id] and t.transaction_date BETWEEN [@range.begin] and [@range.end]

Thanks so much!

  • 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-27T15:40:52+00:00Added an answer on May 27, 2026 at 3:40 pm

    The easiest thing here might be to add

    has_many :children_timesheets, :through => :merged_children, :source => :timesheets
    

    To your employee model,

    Then (assuming in_range is actually a scope, or a class method that does a find)

    children_timesheets.in_range(...)
    

    Should be the collection of timesheets you’re interested in and you can do something like

    children_timesheets.in_range(...).collect(&:hours_minutes).sum
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry for asking silly question.. I have an array with class objects like: Class
Sorry for asking what seems like such an obvious question. I have an adapter
Sorry for asking a totally basic question, but if I have a synthesized property
Sorry for asking a basic question like this but I've been reading on this
I'm terribly sorry for asking yet another anonymous-function question, but it seams like every
Sorry for asking very basic question. I would like to set OR condition in
Guys, sorry for asking basic question, i have a problem here where I have
Sorry for asking what is probably a very simple noob question. I have a
Yeah, sorry about asking a stupid n00b question. So I have a C# program.
Sorry for asking a stupid question but I have a problem that makes me

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.