# Object/Record foreign key IDs set to zero

**URL:** <https://www.ruby-forum.com/t/object-record-foreign-key-ids-set-to-zero/192843>\
**Category:** Rails\
**Created:** [August 6, 2010, 8:29pm UTC](https://www.ruby-forum.com/t/object-record-foreign-key-ids-set-to-zero/192843 "2010-08-06T20:29:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![iaindooley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/df788c/32.png) [@iaindooley](https://www.ruby-forum.com/u/iaindooley)\
**Post date:** [August 6, 2010, 8:29pm UTC](https://www.ruby-forum.com/t/object-record-foreign-key-ids-set-to-zero/192843/1 "2010-08-06T20:29:03Z")

</div>

Hello,

Hope no one minds me just jumping in here with a question.

I’m completely new to Ruby on Rails. I’ve been reading “Simply Rails  
2”, and following their examples. I reached a point where the unit  
tests were unexpectedly failing. Upon investigation, I discovered that  
when Rails loads the fixtures, the foreign keys aren’t being populated  
with the foreign record’s ID, but instead is simply 0 (discovered this  
by looking at the tables manually).

The example has two models: stories and votes. One story–\>many votes  
relationship.

The stories fixture:  
one:  
name: My shiny weblog  
link: [http://poocs.net/](http://poocs.net/)

two:  
name: SitePoint Forums  
link: [SitePoint Forums | Web Development & Design Community](http://www.sitepoint.com/forums/)

The votes fixture:  
one:  
story\_id: one

two:  
story\_id: one

three:  
story\_id: two

four:  
story\_id: two

You’d expect that ‘story\_id’ for two of the records in the votes table  
would be set to the corresponding ID for the first story, and so on,  
but what we find instead is that for all the records, the story\_id is  
zero (which is why some of my assertions fail, since it can’t find the  
votes that should be associated with a given story).

Presumably, I’ve made an error in either my fixtures or the models,  
I’ve gone back over their examples and haven’t been able to find it;  
presumably I’m not seeing it for the forest or something. The models,  
in case the error is there:

class Story \< ActiveRecord::Base  
validates\_presence\_of :name, :link  
has\_many :votes do  
def latest  
find :all, :order =\> ‘id DESC’, :limit =\> 3  
end  
end  
def to\_param  
“#{id}-#{name.gsub(/\W/, ‘-’).downcase}”  
end  
end

class Vote \< ActiveRecord::Base  
belongs\_to :story  
end

Thanks,

Iain

---

<div class="post-metadata">

**Author:** ![iaindooley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/df788c/32.png) [@iaindooley](https://www.ruby-forum.com/u/iaindooley)\
**Post date:** [August 6, 2010, 8:30pm UTC](https://www.ruby-forum.com/t/object-record-foreign-key-ids-set-to-zero/192843/2 "2010-08-06T20:30:49Z")

</div>

On Aug 6, 2:28 pm, Iain D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The votes fixture:  
> one:  
> story\_id: one

That should be story: one if you want rails to fill in the foreign  
keys for you.

Fred

---

<div class="post-metadata">

**Author:** ![iaindooley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/df788c/32.png) [@iaindooley](https://www.ruby-forum.com/u/iaindooley)\
**Post date:** [August 6, 2010, 8:43pm UTC](https://www.ruby-forum.com/t/object-record-foreign-key-ids-set-to-zero/192843/3 "2010-08-06T20:43:01Z")

</div>

Iain D. wrote:

> Hello,
> 
> Hope no one minds me just jumping in here with a question.
> 
> I’m completely new to Ruby on Rails. I’ve been reading “Simply Rails  
> 2”, and following their examples. I reached a point where the unit  
> tests were unexpectedly failing. Upon investigation, I discovered that  
> when Rails loads the fixtures, the foreign keys aren’t being populated  
> with the foreign record’s ID, but instead is simply 0 (discovered this  
> by looking at the tables manually).  
> […]

Slightly off topic, but: once you finish the tutorial, forget you ever  
heard of fixtures. For a whole lot of reasons, they represent a poor  
approach to testing. Use factories instead (I recommend Machinist) and  
your tests will be much less troublesome.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![iaindooley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/df788c/32.png) [@iaindooley](https://www.ruby-forum.com/u/iaindooley)\
**Post date:** [August 6, 2010, 9:21pm UTC](https://www.ruby-forum.com/t/object-record-foreign-key-ids-set-to-zero/192843/4 "2010-08-06T21:21:10Z")

</div>

On Fri, Aug 6, 2010 at 1:29 PM, Frederick C.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Aug 6, 2:28 pm, Iain D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > The votes fixture:  
> > one:  
> > story\_id: one
> 
> That should be story: one if you want rails to fill in the foreign  
> keys for you.

Oh! I tried that and it cleared up my problems. So I’m telling the  
vote with the label ‘one’ that it is associated with the story with  
the label ‘one’? And Rails does the rest?

Iain

---

<div class="post-metadata">

**Author:** ![iaindooley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/df788c/32.png) [@iaindooley](https://www.ruby-forum.com/u/iaindooley)\
**Post date:** [August 6, 2010, 10:50pm UTC](https://www.ruby-forum.com/t/object-record-foreign-key-ids-set-to-zero/192843/5 "2010-08-06T22:50:00Z")

</div>

Iain D. wrote:

> Oh! I tried that and it cleared up my problems. So I’m telling the  
> vote with the label ‘one’ that it is associated with the story with  
> the label ‘one’? And Rails does the rest?

Yes.

And +1 for following Marnen’s advice on using factories (like  
Machinist).
