Helo, I had stuck with this problem.
Just to make things simple i’ll explain it shorter.
Student has_many :grades
belongs_to :class
Grade belongs_to :student
belongs_to :rubric
Rubric belongs_to :course
has_many :students, :through => :class
To create new grade we need to find existent students. We can call @class.students to do so. Also, grade should be created with rubric_id.
I’ve already found out how to do nested forms. But how to pass plural student id’s to new grades?
How to build controller and view to do so?
(Arg, I can’t format my question properly in the comments, so I have to use an answer. Bear with me please because I don’t think it answers your question yet.)
I don’t think your model is setup properly to achieve that. In the scenario you are asking, Student and Grade seems to be in many-to-many association. I.e. a student has many rubric, but a rubric has many students, both through grade (which is your association model). Did I understand this correct?
Let me know if my interpretation above is correct and I can follow up with an answer on the controller and the view. I recently did something very similar for another project.
EDIT: Now that we have clarified the problem, here’s my suggested answer.
The goal is to assign multiple students for a rubric in a particular class.
There is no built-in methods that will support something like this so we will have to build the records and the association with the right controller action and form.
In Grades model we add this function:
def self.checked?(class_id, student_id, rubric_id)
Grade.find(:all, :conditions => {:class_id => class_id, :student_id => student_id, :rubric_id => rubric_id}).present?
end
In the form_for :rubric, we create something like this:
Now in the RubricsController’s create action you should have a params[:grades] which is a hash with key [:class] whose value is should just be an array of student ids. Now you can create the grades object that associates these models since you have a rubric id (@rubric.id), a class id (@class.id) and an array of student ids.
I hope I didn’t screw the code above since I had to change it from my current implementation. But looking at the console log of the rails server should allow you to figure out what parameters are passed when the above form is submitted and make adjustments.