On 16 Sep 2008, at 19:34, Ad Richards wrote:

>
> Being a rails newbie, I appear to have hit a large wall.
>
> I am trying to create an online gradebook using rails, where I and  
> other
> teachers at my school can record students' grades for all their  
> homework
> assignments.
>
> students           assessment1         assessment2         assessment3
> --------           -----------         -----------         -----------
> Bart Simpson          12                    15                  9
> Lisa Simpson          7                     6                   14
> Charlie Brown         12                    14                  17
>
> I would like teachers to be able to dynamically add columns for new
> assessments as they occur, and understand that this is not such a  
> simple
> task.
>

This doesn't have to be very complicated. If we forget for a second  
about the layout of the table, this would output students and their  
grades

<% @students.each do |student|%>
   <%= h student.name %>:
   <% student.gradations.each do |gradation| %>
      <%= h gradation.assignment.name %>: <%= gradation.grade %>
   <% end %>
<% end %>

Your layout is a bit more complicated because you need to iterate over  
the gradations in the same assignment order for everyone, cope with  
people not having done an assignment yet etc.

I might do something like this:

in your model:

def gradation_hash
   @gradation_hash ||= gradations.index_by {|g| g.assignment_id }
end

in your controller

@assignments = Assignment.find :all
@students = Student.find :all

in your view

<tr><td></td>
   <% @assignments.each do |assignment| %>
     <td>
       <%= h assignment.name %>
     </td>
   <% end %>
</tr>

<% @students.each do |student| %>
<tr>
   <td> <%= h student.name %> </td>
   <% @assignments.each do |assignment| %>
     <td>
       <% if g = student.gradation_hash[assignment.id] %>
         <%= h g.grade %>
       <% end %>
     </td>
   <% end %>
</tr>
<% end >


Fred







> It is obvious that I need a join model in which to store the grades as
> there will be many students and many assignments, and each grade  
> will be
> linked to both a specific assignment and specific student.
>
> So I have written my models as follows:
>
>   1. class Student
>   2. has_many :gradations
>   3. has_many :assignments, :through => :gradations
>   4. end
>   5. class Gradation
>   6. belongs_to :student
>   7. belongs_to :assignment
>   8. end
>   9. class Assignment
>  10. has_many :gradations
>  11. has_many :students, :through => :gradations
>  12. end
>
> So far so good right?
> Well I have since spent an infuriating amount of time fiddling about
> querying the models, and I can't for the life of me figure out how to
> pull out the data as in the table above. i.e. listing all the  
> students'
> grades for each assignment.
> I wonder if there is somebody out there who can put me out of my  
> misery.
> Or maybe I am barking up the completely wrong tree? Is there an easier
> way of doing this that I have totally overlooked?..
> Please, somebody help me. I don't want to lose any more hair over  
> this.
>
> I have posted a more detailed picture of my dilemma at
> http://railspool.blogspot.com/
> -- 
> Posted via http://www.ruby-forum.com/.
>
> >


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to