Question: A question that I hope you can answer for a Q&A app. Still very new with Rails. It should be fairly simple but there is a small issue that I run into whenever I am trying to create a list of links to show up that point to a RESTful route from a loop. 

Here's the code for the controller:

users\_controller.rb

```
def list
  @user = User.find(params[:id])
  @users = User.find(:all, :select => :name)
end

def quiz
  @user = User.find(params[:id])
  @users = User.find(:all, :select => :name)
end

```

Here's the code for the view:

```
<% if current_user.admin? %>
  <ul>
    <%- @users.each do |link| %>
      <li><%= link_to link.name, quiz_user_path(@user, @quiz) %></li>
    <% end %>
  </ul>
<% end %>

```

Here's the code for the route:

```
resources :users do 
  get 'quiz', :on => :member
end

```

What I want to do is generate individual links based on the name of the users and then link to the quiz page for that specific user. I'm pretty sure that something needs to be changed for the code in my view. Right now, all I'm getting is a link that all points to the current user which in this case is user 4. (`http://localhost:3000/users/4/quiz`)

Thanks for any quick tips to solve this.

Answer: 
You can certainly do this with a simple UPDATE:

```
UPDATE table SET Temp6MonthHighAverage = 
  (SELECT AVG(HighTemp) FROM table t2 WHERE
     t2.LogDate <= table.LogDate
     AND t2.LogDate > DATEADD(m, -6, table.LogDate)
  )

```

To avoid re-calculating constantly (since the past will not change), just add a `WHERE Temp6MonthHighAverage IS NULL` at the end and the same UPDATE can be run as needed to fill in the gaps as new dates are added.
Have a look at something like this

```
DECLARE @Table TABLE(
    LogDate DATETIME,
    HighTemp INT,
    Temp6MonthHighAverage INT
)

INSERT INTO @Table SELECT '01 Jan 2000', 15, NULL
INSERT INTO @Table SELECT '01 May 2000', 14, NULL
INSERT INTO @Table SELECT '01 Jun 2000', 13, NULL
INSERT INTO @Table SELECT '01 Jul 2000', 12, NULL
INSERT INTO @Table SELECT '01 Dec 2000', 17, NULL

SELECT  *
FROM    @Table

;WITH DistinctDates AS (
        SELECT  DATEADD(month,-6,LogDate) StartDate,
                LogDate EndDate,
                HighTemp
        FROM    @Table
)
, Aggregates AS (
        SELECT  dd.EndDate LogDate,
                dd.HighTemp,
                MAX(t.HighTemp) Temp6MonthHighAverage
        FROM    DistinctDates dd LEFT JOIN
                @Table t    ON  t.LogDate BETWEEN dd.StartDate AND dd.EndDate
        GROUP BY    dd.EndDate,
                    dd.HighTemp
)
UPDATE  @Table
SET     Temp6MonthHighAverage = a.Temp6MonthHighAverage
FROM    @Table t INNER JOIN
        Aggregates a    ON  t.LogDate = a.LogDate

SELECT  *
FROM    @Table

```