# Creating intances of a model with two required foreign keys

**URL:** <https://www.ruby-forum.com/t/creating-intances-of-a-model-with-two-required-foreign-keys/122540>\
**Category:** Rails\
**Created:** [December 9, 2007, 2:02am UTC](https://www.ruby-forum.com/t/creating-intances-of-a-model-with-two-required-foreign-keys/122540 "2007-12-09T02:02:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c89c15/32.png) [@tomn](https://www.ruby-forum.com/u/tomn)\
**Post date:** [December 9, 2007, 2:02am UTC](https://www.ruby-forum.com/t/creating-intances-of-a-model-with-two-required-foreign-keys/122540/1 "2007-12-09T02:02:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tomn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c89c15/32.png) [@tomn](https://www.ruby-forum.com/u/tomn)\
**Post date:** [December 9, 2007, 11:59am UTC](https://www.ruby-forum.com/t/creating-intances-of-a-model-with-two-required-foreign-keys/122540/2 "2007-12-09T11:59:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tomn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c89c15/32.png) [@tomn](https://www.ruby-forum.com/u/tomn)\
**Post date:** [December 9, 2007, 9:23pm UTC](https://www.ruby-forum.com/t/creating-intances-of-a-model-with-two-required-foreign-keys/122540/3 "2007-12-09T21:23:34Z")

</div>

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)
