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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:32:33+00:00 2026-05-30T05:32:33+00:00

I am trying to create a Redmine plugin built on Ruby on Rails. I

  • 0

I am trying to create a Redmine plugin built on Ruby on Rails. I have the following query which fetches the sum of time_entries of all the issues during an invoice.

SELECT SUM( t.hours ) 
FROM time_entries t
JOIN invoices i ON t.project_id = i.project_id
WHERE t.project_id = <current project id----@project.id>
AND i.id = <billing invoice id>
AND t.updated_on >  'i.created_at'
AND t.updated_on < 'i.due_date'

How can I store this query data inside invoices table column called time_spent or retrieve the results in the invoices view which lists all invoices along with the above query by invoice ID

The associations in model I created goes like this

class Invoice < ActiveRecord::Base
  set_table_name "invoices"
    set_primary_key "invoice_id"

    belongs_to :project
    belongs_to :author, :class_name => "User", :foreign_key => "author_id"
    has_many :time_entries, :class_name => "TimeEntry", :foreign_key => "project_id"

In the Controller I am calling the model as

@invoices = Invoice.find(:all, :joins=>" INNER JOIN time_entries ON time_entries.project_id = invoices.project_id",:conditions => ["invoices.project_id = ? AND updated_on > ? AND updated_on < ?", @project.id, invoices.created_at, invoices.due_date])

I know the controller instance variable is totally messed up.

Somewhere I doing mistake. Can some one please help me with this.

  • 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-30T05:32:34+00:00Added an answer on May 30, 2026 at 5:32 am

    I’m assuming Rails 3+ and MySQL, this should give you your each item in your @invoices collection an accessor named “time_spent” which will have the sum you are looking for. It will not persist this info in the db, but it retrieves it for your view:

    Invoice.where("invoices.project_id = ? AND invoices.updated_on > ? AND invoices.updated_on < ?", @project.id, invoices.created_at, invoices.due_date).select("SELECT invoices.*, SUM( time_entries.hours ) as time_spent").joins(:time_entries).group("invoices.id")
    

    I’m guessing at < Rails 3 below:

    Invoice.find(:all, 
    :conditions => ["invoices.project_id = ? AND updated_on > ? AND updated_on < ?", @project.id, invoices.created_at, invoices.due_date],
    :select=>"SELECT invoices.*, SUM( time_entries.hours ) as time_spent",
    :joins=>[:time_entries],
    :group=>"invoices.id")
    

    (hoping I typed this correctly. If not, the gist is to use the “select” and “group” methods to get your result.)

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

Sidebar

Related Questions

Trying to create a QtRuby application, I get the following error: /usr/lib64/ruby/site_ruby/1.8/Qt/qtruby4.rb:2144: [BUG] Segmentation
I have been trying to create the development and test databases in Redmine, with
I am trying create a data.frame from which to create a graph. I have
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can
Trying to create a list to return some JSON data to a view. Following
hi I'm trying create chat using node.js I see example in http://chat.nodejs.org/ I have
All, I am trying create a 'to & fro' animation using jquery animate &
Trying to create the following routine in MySQL Workbench yields a This object's DDL
Today I was trying to install Redmine on my shared hosting following this guide:
I am trying create a search box which will search datatables. The search box

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.