Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>as @Christoph said, if there is a lot of logic involved, implement this as a method in the model ; moreover you could implement this as a <a href="http://api.rubyonrails.org/classes/ActiveRecord/NamedScope/ClassMethods.html#method-i-scope" rel="nofollow">scope</a> :</p> <pre><code>class RptNote &lt; ActiveRecord::Base def self.performance_metric_notes( past_weeks = 4 ) # this will calculate the last sundays for this week + # the nth past weeks, defaults to 4 : last_sundays = (0..past_weeks).map do |n| (Time.now.beginning_of_week - (1+n*7).days).strftime("%Y-%m-%d") end # this will return a relation. Using an array in a where clause # renders a SQL IN selector. self .where( week_of: last_sundays ) .order( :week_of, :market, :measure ) end end </code></pre> <p>... this would even let you refine the results by adding other clauses :</p> <pre><code>RptNote.performance_metric_notes.where( measure: something ) </code></pre> <p>see <a href="http://railscasts.com/episodes/215-advanced-queries-in-rails-3" rel="nofollow">Railscasts #215</a> for more info about relations and queries in rails 3</p> <p><strong>edit :</strong> wooops, my bad, just saw your question was on rails 2. The logic stays the same, except for the query. </p> <p>There is more : if your controller actions use this a lot, you can set a <a href="http://railsbrain.com/api/rails-2.2.2/doc/index.html?a=M000259&amp;name=before_filter" rel="nofollow">before_filter</a> on the controller to auto-fetch the records :</p> <pre><code>before_filter :fetch_notes def fetch_notes @notes = RptNote.performance_metric_notes end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload