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: 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Aug 6, 2:28 pm, Iain Davis <iedav...-MyONv+wJkRE54bBUE1Kujg@public.gmane.org> 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Fri, Aug 6, 2010 at 1:29 PM, Frederick Cheung <frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > On Aug 6, 2:28 pm, Iain Davis <iedav...-MyONv+wJkRE54bBUE1Kujg@public.gmane.org> 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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). -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.