Roggie,
Try:
@m = Score.find(:first, :conditions => ["student_id = ?", params[:id]] , :order => "recorded_on desc", :limit => 1)
The idea is to get all records for that student, sort them in descening order by date and get the first one. Which will be the most recent.
In general though, I imagine you already know which student you're working with. Lets say you have a Student instance in @student. Then you could do this:
student.scores.find(:conditions => "task_id = 3", :order => "recorded_on desc", :limit => 1)
Now lets assume you have a student, and you already know which task you are interested in. Assuming your model describes this:
student.tasks[some_task].scores(order => "recorded_on desc", :limit => 1)
All of these are a bit too "intimate" with the database mechanism so would be better off in the model. A bit like this (not literally this code)
class Student < ActiveRecord::Base has_many :tasks has_many :scores, :through => :tasks do def recent_score find(:first, :order "recorded_on desc", :limit => 1) end end end
Then you can just do: @student = Student.find(:first) @recent_score = @student.tasks[@task_index].scores.recent_score
Note that @task_index in these cases is not the task number, but the index into the array of tasks on the association proxy. You would need to work out which entry you need, or to resolve the correct task in advance.
Lastly, you could then
@student.tasks.each do |task| puts "Recent score for task #{task.name}" puts "Score: ", task.scores.recent_score.score puts "Recorded on: ", task.scores.recent_score.recorded_on end
and you are all done (I hope!)
Cheers, --Kip
Cheers, --Kip