Object/Record foreign key IDs set to zero

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/

two:   name: SitePoint Forums   link: SitePoint Forums | Web Development & Design Community

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

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

Fred

Iain Davis 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,

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

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