Created
November 1, 2012 00:32
-
-
Save larskluge/3990887 to your computer and use it in GitHub Desktop.
Mongoid: order of referenced objects in many-to-many relation not as expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'mongoid' | |
| puts "RUBY_VERSION: #{RUBY_VERSION}" | |
| puts "Mongoid::VERSION: #{Mongoid::VERSION}" | |
| puts | |
| Mongoid.configure do |cfg| | |
| cfg.connect_to 'apples_and_oranges' | |
| end | |
| class Apple | |
| include Mongoid::Document | |
| has_and_belongs_to_many :oranges | |
| end | |
| class Orange | |
| include Mongoid::Document | |
| has_and_belongs_to_many :apples | |
| end | |
| a1, a2 = Apple.new, Apple.new | |
| # behaviour changes when oranges are not persisted to db! (use .new instead of .create to reproduce) | |
| o1, o2 = Orange.create!, Orange.create! | |
| a1.oranges = [o1, o2] | |
| p a1.orange_ids # correct | |
| p a1.oranges.map(&:id) # correct | |
| puts | |
| a2.oranges = [o2, o1] | |
| p a2.orange_ids # correct | |
| p a2.oranges.map(&:id) # wrong |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment