#986 confirmed
Phil Wang

habtm fails for two level SEL

Reported by Phil Wang | July 21st, 2009 @ 07:38 PM

If you have two classes Apple and Oranges that are in HABTM, doing the following fails.

Apple.each do |a|
a.oranges.each do |o|

p "Apple #{a.id} owns Orange #{o.id}"

end end

The oranges that each apple owns is not displayed correctly.

Comments and changes to this ticket

  • Phil Wang

    Phil Wang July 21st, 2009 @ 10:32 PM

    yea, i just quickly retested this on Sinatra. definitely a bug.

  • Phil Wang

    Phil Wang July 21st, 2009 @ 10:33 PM

    class Apple
      include DataMapper::Resource
      property :id,         Serial
      has n, :oranges, :through=>Resource  
    end
    
    class Orange
      include DataMapper::Resource
      property :id, Serial
      has n, :apples, :through=>Resource
    end
    
    DataMapper.auto_migrate!
    
    Apple.create
    Apple.create
    Apple.create
    Orange.create
    Orange.create
    Orange.create
    a = Apple.get(1)
    o = Orange.get(1)
    a.oranges << o
    a.save
    a = Apple.get(2)
    o = Orange.get(2)
    a.oranges << o 
    a.save
    a = Apple.get(3)
    o = Orange.get(2)
    a.oranges << o
    a.save
    
    o = Orange.get(1)
    a.oranges<<o
    a.save
    
    Apple.all.each do |a|
      a.oranges.each do |o|
        p "#{a.id} #{o.id}"
      end
    end
    
  • Martin Gamsjaeger (snusnu)

    Martin Gamsjaeger (snusnu) July 21st, 2009 @ 11:18 PM

    • Assigned user set to “Dan Kubb (dkubb)”
    • State changed from “new” to “confirmed”

    I can confirm this on latest next branch. I put together a standalone based on the above snippet and added a few comments on what seems to happen.

    require 'rubygems'
    require 'dm-core'
    
    DataMapper::Logger.new(STDOUT, :debug)
    DataMapper.setup(:default, 'sqlite3:memory:')
    
    class Apple
      include DataMapper::Resource
      property :id,         Serial
      has n, :oranges, :through=>Resource  
    end
    
    class Orange
      include DataMapper::Resource
      property :id, Serial
      has n, :apples, :through=>Resource
    end
    
    DataMapper.auto_migrate!
    
    Apple.create;  Apple.create;  Apple.create
    Orange.create; Orange.create; Orange.create
    
    a = Apple.get(1)
    o = Orange.get(1)
    a.oranges << o
    a.save
    a = Apple.get(2)
    o = Orange.get(2)
    a.oranges << o 
    a.save
    
    a = Apple.get(3)
    
    o = Orange.get(1) # orange_1 is already in apple_1.oranges (doesn't end up in output - FAIL)
    a.oranges<<o
    a.save
    o = Orange.get(2) # orange_2 is already in apple_2.oranges (doesn't end up in output - FAIL)
    a.oranges << o
    a.save
    o = Orange.get(3) # orange_3 isn't part of any oranges collection yet (ends up in output - PASS)
    a.oranges << o
    a.save
    
    Apple.all.each do |a|
      puts "apple_id = #{a.id}"
      a.oranges.each do |o|
        p "apple_#{a.id}.oranges = #{o.id}"
      end
    end
    
    # apple_id = 1
    # "apple_1.oranges = 1"
    # apple_id = 2
    # "apple_2.oranges = 2"
    # apple_id = 3
    # "apple_3.oranges = 3"
    
  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) July 22nd, 2009 @ 03:56 PM

    • State changed from “confirmed” to “accepted”
  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) August 15th, 2009 @ 12:09 PM

    • Milestone changed from 0.10.0 to 0.10.1

    It is unlikely this will be ready for the 0.10.0 release. Bumping to 0.10.1 milestone

  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) October 4th, 2009 @ 09:33 PM

    • Milestone changed from 0.10.1 to 0.10.2

    [project:id#20609 not-tagged:"0.10.0" not-tagged:"0.10.1" milestone:id#51895 bulk edit command]

  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) February 1st, 2010 @ 04:33 PM

    • Milestone changed from 0.10.2 to 1.0.0
  • Dan Kubb (dkubb)
  • Dan Kubb (dkubb)
  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) February 2nd, 2010 @ 02:47 AM

    • State changed from “accepted” to “confirmed”

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 »

Pages