Creating intances of a model with two required foreign keys

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).

On 9 Dec 2007, at 01:02, Tom N. wrote:

and I’d like to initialize the name attribute of course_offering to
match the name attribute of the parent Student.

Just a guess, but the fact that you aren’t using the expected
convention for class names isn’t going to help (Those should be
CourseRecord, CourseOffering)

Unfortunately what I have below isn’t working. I get errors of
“Expected .\app\models\course_record.rb to define Course_record”.
That usually means that either your naming has confused rails or that
file contains a syntax error of some sort ( ruby -c checks a file for
syntax errors)

Fred

Thank you Fred, you helped me find a main error I was making.

I was following conventions shown in the AWDwR book where they ran
ruby script/generate model line_item (pg 132)

Their example shows it in lower case with a spacer. Is this a bad
habit? I seem to recall the script automatically downcasing and spacing
camel back names anyway.

But what I really missed was that the script/generate model creates a
class named LineItem even though the table for migrations is called
line_items and the model folder would be named line_items.

My examples were course_offering, with a generated class CourseOffering
NOT what I was typing: Course_offering.new (there was no such class
hence the error).

Thanks again for helping out. Its a funny little quirk to remember
where the class name is different from the names of the related folders
and database tables. It hadn’t been an issue with my single word
classes.

(btw I went through replaced all the objects in session with object ids
as you and others warned me about the other day, I welcome any other
tips about bad habits you see)

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs