Monday 15 February 2010

postgresql - Rails named scope for left joined model -


I have a table named acts , and I will run a query that rolls a whole week Work for values ​​I want to make sure that the question always gives a line for each day of the week, even if there is no record for that day. Right now I am doing this like this:

  def self.this_week_totals Sunday = time.non.bizning_of_week (: Sunday) .strftime ("% Y-% m-% d") connection. Select_values ​​(& lt; & lt; -EOQ) as total_time to gener_series (0, 6) as SLECT COALESCE (SUM (end_time - start_time, '0:00:00')) # {Sunday} ': Date + STE WHERE deleted_at IS NULL Group by St IOK End   

Is there any way that I can make it a nominated scope on ACT class? Can be added with, for example To filter the act by client_id ? Since work is not in my FROM , but is part of LEFT JOIN , I'm not guessing, but maybe someone knows a The way.

EDIT: Just to clarify, for this method, always will return exactly 7 working objects, whether in the database.

If you want your query object to be a series, then it must be an ActiveRelation object

where , select , order and other Arab objects return from activation to activation objects, so if working below Then you can close the series of returned query objects

Note 3 and above, a class method giving an active method Is basically like a radius, they are both series related query objects

  class def def.this_week_totals sunday = Time.now.beginning_of_week (: Sunday) .strftime ("% Y-% M-% d ") (" Total_time "as Coal (SUM (end_time - start_time), '0:00:00') .from (" s (t) as gener_series (0, 6) " ). See ("Fidelity works in acts.day = '# {sunday}': date + st"). Where ("deleted_at IS NULL") .group ("st") .order ("st") end # ... end client_id = params [: client_id] Act.this_week_totals.where ("client_id =?", Client_id) < / Code>  



No comments:

Post a Comment