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