Hi, I have the following code in a RoR project, models and tests are all in the proper directories: require File.dirname(__FILE__) + ''/../test_helper'' class GalleryTest < Test::Unit::TestCase fixtures :galleries @gallery = nil def test_presence_of @gallery = Gallery.new @gallery.title = ''My test gallery'' @gallery.nr = 123 image = Image.new(1, 100, ''jpg'') @gallery.images = Array.new @gallery.images << image @gallery.save assert @gallery.valid? end end class Gallery < ActiveRecord::Base validates_presence_of :title, :nr, :images validates_numericality_of :nr validates_length_of :title, :in => 1..255 has_many :images end class Image < ActiveRecord::Base validates_presence_of :nr, :sort_nr, :ext validates_presence_of :sort_nr validates_presence_of :ext belongs_to :gallery def initialize(nr, sort_nr, ext) @nr = nr @sort_nr = sort_nr @ext = ext end end When I run the gallery test file, ruby shows me the following error: test_presence_of(GalleryTest): NoMethodError: You have a nil object when you didn''t expect it! You might have expected an instance of Array. The error occurred while evaluating nil.[] c:/program files/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.6/lib/active_ record/base.rb:1871:in `read_attribute'' ... record/transactions.rb:121:in `transaction'' c:/program files/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.6/lib/active_ record/transactions.rb:129:in `save'' test/unit/gallery_test.rb:23:in `test_presence_of'' It has something to do with gallery.images, but I can''t figure out how to fix this. Another question (offtopic): Why does it take 10 seconds for the test to start running? This gets annoying when you want to "code a little, test a little". -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-May-15 09:19 UTC
Re: ActiveRecord - nil object when you didn''t expect it
On 15 May 2008, at 06:40, Aa Aa wrote:> > Hi, > class Image < ActiveRecord::Base > validates_presence_of :nr, :sort_nr, :ext > validates_presence_of :sort_nr > validates_presence_of :ext > > belongs_to :gallery > > def initialize(nr, sort_nr, ext) > @nr = nr > @sort_nr = sort_nr > @ext = ext > end > endYou''re overriding activerecord''s initialize function, but without calling super, so stuff is not initialized properly (and thus crazy stuff happens. In addition activerecord attributes are not persisted as instance variables so you inititialize function doesn''t do what you think it does. Why do you need a custom initialize at all ?> > > Another question (offtopic): Why does it take 10 seconds for the > test to > start running? This gets annoying when you want to "code a little, > test > a little".Probably because certain bits of ruby are a lot slower on windows (there was another thread about this a day or 2 ago) 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On 15 May 2008, at 06:40, Aa Aa wrote: > >> @nr = nr >> @sort_nr = sort_nr >> @ext = ext >> end >> end > > You''re overriding activerecord''s initialize function, but without > calling super, so stuff is not initialized properly (and thus crazy > stuff happens. In addition activerecord attributes are not persisted > as instance variables so you inititialize function doesn''t do what you > think it does. Why do you need a custom initialize at all ? > >> >> >> Another question (offtopic): Why does it take 10 seconds for the >> test to >> start running? This gets annoying when you want to "code a little, >> test >> a little". > Probably because certain bits of ruby are a lot slower on windows > (there was another thread about this a day or 2 ago) > > FredCan''t test it right now, but super could be the issue indeed. I did a initialize override so I could do this my_image = Image.New(123, 1, ''jpg'') instead of my_image = Image.New my_image.nr = 123 my_image.sort_nr = 1 my_image.ext = ''jpg'' Is there another way to send arguments to initialize without doing an override? -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-May-15 10:06 UTC
Re: ActiveRecord - nil object when you didn''t expect it
On 15 May 2008, at 11:01, Aa Aa wrote:> > Can''t test it right now, but super could be the issue indeed. I did a > initialize override so I could do this > > my_image = Image.New(123, 1, ''jpg'') > > instead of > > my_image = Image.New > my_image.nr = 123 > my_image.sort_nr = 1 > my_image.ext = ''jpg'' > > Is there another way to send arguments to initialize without doing anImage.new :nr => 123, :sort_nr => 1, :ext => ''jpg'' 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On 15 May 2008, at 11:01, Aa Aa wrote: > >> my_image.sort_nr = 1 >> my_image.ext = ''jpg'' >> >> Is there another way to send arguments to initialize without doing an > > Image.new :nr => 123, :sort_nr => 1, :ext => ''jpg'' > > FredThanks Fred, the test is working now. I''ve removed the initialize and used the => thing instead. -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---