#893 ✓resolved
Kieran Huggins

Saving silently fails with Discriminator

Reported by Kieran Huggins | June 8th, 2009 @ 09:43 AM | in 0.10.0

When using a discriminator, models loaded with the parent class (Product#first) will silently fail to save as their specific type (Book < Product).

Comments and changes to this ticket

  • ronin-15404 (at lighthouseapp)

    ronin-15404 (at lighthouseapp) June 8th, 2009 @ 10:12 AM

    It actually seems to be the validation that's doing the SELECT. If you use #save! instead of #save, it will update properly. If you run book.valid? the SELECT will come up and overwrite the change.

  • ronin-15404 (at lighthouseapp)

    ronin-15404 (at lighthouseapp) June 8th, 2009 @ 10:14 AM

    • Tag changed from datamapper, discriminator to datamapper, discriminator, validations
  • Martin Gamsjaeger (snusnu)

    Martin Gamsjaeger (snusnu) July 9th, 2009 @ 02:52 PM

    • State changed from “new” to “resolved”
    • Milestone set to 0.10.0

    I just ran that script using current next branches and I cannot reproduce the error. Here's what I get (I had to change the require statements to require "dm-core" instead of require "datamapper" inside your dm-bug.rb file). I'm marking this as resolved for 0.10, feel free to reopen the discussion.

    mungo:Downloads snusnu$ ruby dm-bug.rb 
    Thu, 09 Jul 2009 19:47:57 GMT ~ debug ~ (0.000181) SELECT sqlite_version(*)
    Thu, 09 Jul 2009 19:47:57 GMT ~ debug ~ (0.000149) DROP TABLE IF EXISTS "products"
    Thu, 09 Jul 2009 19:47:57 GMT ~ debug ~ (0.000033) PRAGMA table_info("products")
    Thu, 09 Jul 2009 19:47:57 GMT ~ debug ~ (0.000499) CREATE TABLE "products" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "price" FLOAT, "type" VARCHAR, "author" VARCHAR(255))
    
    book = Book.create(:price=>9.99,:author=>"me")
    Thu, 09 Jul 2009 19:47:57 GMT ~ debug ~ (0.000073) INSERT INTO "products" ("price", "type", "author") VALUES (9.99, 'Book', 'me')
    => #<Book @id=1 @price=9.99 @type=Book @author="me">
    
    book = Product.first
    Thu, 09 Jul 2009 19:47:57 GMT ~ debug ~ (0.000067) SELECT "id", "type", "price" FROM "products" WHERE "type" IN ('Product', 'Book') ORDER BY "id" LIMIT 1
    => #<Book @id=1 @price=9.99 @type=Book @author=<not loaded>>
    
    book.price = "$5.50"
    => $5.50
    
    book.save
    Thu, 09 Jul 2009 19:47:57 GMT ~ debug ~ (0.000052) UPDATE "products" SET "price" = '$5.50' WHERE "id" = 1
    => true
    
    book
    => #<Book @id=1 @price="$5.50" @type=Book @author=<not loaded>>
    

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 »

Attachments

Pages