Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>(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.)</p> <p>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?</p> <pre><code>Class has_many :students has_many :grades has_many :rubrics, :through =&gt; :grade Student has_many :grades has_many :rubrics, :through =&gt; :grade belongs_to :class # A grade is a particular rubric assigned to a student for a particular class Grade belongs_to :student belongs_to :rubric belongs_to :class Rubric belongs_to :course has_many :grades has_many :students, :through =&gt; :grade </code></pre> <p>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.</p> <p>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.</p> <p>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.</p> <p>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</p> <p>In the form_for :rubric, we create something like this:</p> <pre><code>&lt;%= form_for @rubric do |f| %&gt; &lt;% for @class.students.each do |student| %&gt; &lt;%= fields_for :grades do |builder| %&gt; &lt;%= check_box_tag "grades[" + @class.id.to_s + "][]", rubric.id, Grade.checked?(@class.id, student.id, @rubric.id) %&gt; &lt;% end %&gt; &lt;%= f.submit "Save" %&gt; &lt;% end %&gt; </code></pre> <p>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. </p> <p>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.</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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