I asked this the other day but perhaps in too wordy a way.
I have a Student model, a Course_offering model, and a Course_record
model ( a table with columns for grades in midterms, final , lab etc).
The course_record belongs_to both an instance of the Student model and
to an instance of the Course_offering model. I need to create a
collection of course_record instances with the appropriate associations
and I’d like to initialize the name attribute of course_offering to
match the name attribute of the parent Student.
Unfortunately what I have below isn’t working. I get errors of
“Expected .\app\models\course_record.rb to define Course_record”.
@students is an array of student objects and @course_offering is a
particular Course_offering object.
@students.each do |student|
course_record_foo = Course_record.new( :name => student.name
:course_offering_id => @course_offering.id,
:student_id => student.id)
Course_record.transaction do
student.course_records << course_record_foo
@course_offering << course_record_foo
end
end
I am worried that I might not even be grasping the basic way of creating
an instance of a model requiring “not null” foreign keys ?
Do I need to be manually be setting the foreign keys using create and
stuff like ( :course_offering_id => @coarse_offering.id, :student_id =>
student.id )? I’m thinking when I associate one it might trigger a save
which would fail because of the other missing foreign key (even in a
transaction).
Is my approach anywhere close to correct and should I be looking for
errors somewhere else perhaps the error is in the database migration
stuff?
Again the error I’m getting is basically: Expected
.\app\models\course_record.rb to define Course_record (Ive checked and
rechecked for spelling errors of the model etc).