#398 ✓resolved
Anthony Williams

Resource#destroy should return false if called on a new record

Reported by Anthony Williams | June 18th, 2008 @ 12:45 PM


Mucking around with some specs today I found that calling #destroy on a new record would raise a nasty exception:

NoMethodError in 'DataMapper::Resource with sqlite3 should return false to #destroy if the resource is new'
undefined method `model' for nil:NilClass
/Users/anthony/Source/dm-core/lib/dm-core/adapters/data_objects_adapter.rb:252:in `delete_statement'
/Users/anthony/Source/dm-core/lib/dm-core/adapters/data_objects_adapter.rb:81:in `delete'
/Users/anthony/Source/dm-core/lib/dm-core/repository.rb:80:in `delete'
/Users/anthony/Source/dm-core/lib/dm-core/resource.rb:267:in `destroy'

Resource#destroy calls #to_query, which returns nil if the resource is not yet saved, which then gets passed down into Repository#delete (and on and on) until DataObjectsAdapter#delete_statement calls nil#model expecting it to have been a Query.

I've fixed this in my fork by getting Resource#destroy to return false if the resource is unsaved: http://github.com/anthonyw/dm-co...

Comments and changes to this ticket

  • Troy K

    Troy K June 25th, 2008 @ 10:24 AM

    • Tag set to dm-core, patch

    This seems to be fixed in the latest pull. Note the first line of def destroy in resource.rb (line #260)

        def destroy
          return false if new_record?
          return false unless repository.delete(to_query)
          @new_record = true
          properties.each do |property|
            # We'll set the original value to nil as if we had a new record
            original_values[property.name] = nil if attribute_loaded?(property.name)
  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) June 25th, 2008 @ 10:40 AM

    • State changed from “new” to “resolved”

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

People watching this ticket