Ruby on Rails
Saturday, March 30, 2013
I found the way to do that as follows:
either by defining a scope in the Timesheet model:
scope :all_by_start_date, joins(:time_entries).select("timesheets.start_date, sum(time_entries.worktime) as total_days").group('timesheets.start_date')
or the same and full console syntax to check it console:
Timesheet.joins(:time_entries).select("timesheets.start_date, sum(time_entries.worktime) as total_days").group('timesheets.start_date')
Hope this helps.
On Saturday, March 30, 2013 6:08:32 PM UTC+1, Serguei Cambour wrote:
The below query fails:Timesheet.joins(:time_entries).select("timesheets.*, sum(time_entries.worktime) as total").group("timesheets. start_date") The models have the following relations:Timesheet < ARhas_many :activities, dependent: :destroy, inverse_of: :timesheethas_many :time_entries, through: :activitiesaccepts_nested_attributes_for :activities, allow_destroy: trueendclass Activity < ARbelongs_to :timesheet, inverse_of: :activitiesbelongs_to :taskhas_many :time_entries, order: :workdate, dependent: :destroy, inverse_of: :activityaccepts_nested_attributes_for :time_entries, allow_destroy: true, reject_if: proc { |a| a[:worktime].blank? }endclass TimeEntry < ARbelongs_to :activity, :inverse_of => :time_entriesendHow is it possible to group all the time sheets by their start_date and sum the work time ?Thank you.
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/oPYeY9Oa6KkJ.
For more options, visit https://groups.google.com/groups/opt_out.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment