RoR ActiveRecord f.select nil method error

Posted by sellis6688 on Stack Overflow See other posts from Stack Overflow or by sellis6688
Published on 2010-03-27T19:27:29Z Indexed on 2010/03/27 19:33 UTC
Read the original article Hit count: 198

Filed under:

Whenever I use an f.select statement to determine assignment_id(or student_id), and I should get a validation error, I get this error instead of the validation message:

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.collect
Extracted source (around line #11):

8:   </p>
9:   <p>
10:     <%= f.label 'Assignment:' %><br />
11:  <%= f.select(:assignment_id, @assignments.collect {|p| [p.ass_num, p.id]})%>
12:   </p>
13:   <p>
14:     <%= f.label 'First Student:' %><br />

My grades model:

class Grade < ActiveRecord::Base
  has_and_belongs_to_many :students
  belongs_to :assignment
  validates_presence_of :score, :assignment_id, :student_id
  validates_numericality_of :score, :greater_than_or_equal_to => 0,
    :less_than_or_equal_to => 100, :allow_nil => true
  validates_uniqueness_of :student_id, :scope => :assignment_id
end

If I use a text_field, I don't get the error... but there's far too many students for that. Neither @assignments nor @students are nil. Any suggestions?

© Stack Overflow or respective owner

Related posts about activerecord