Last active
August 29, 2015 14:13
-
-
Save ales-vilchytski/b462cd8e2ff607fccd44 to your computer and use it in GitHub Desktop.
Mongoid 4.0.0 accept_nested_attributes_for issue (#3920)
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' | |
| require 'embed' | |
| class Doc | |
| include Mongoid::Document | |
| field :_id, type: String | |
| field :f1, type: String | |
| field :f2, type: String | |
| embeds_many :embeds | |
| accepts_nested_attributes_for :embeds, allow_destroy: true, reject_if: lambda{ |attrs| puts 'This is "REJECT_IF", it\'s never called on update, but called on assign' } | |
| def to_s | |
| "#{self.inspect} /// Embeds count: #{self.embeds.size}: #{self.embeds.inspect}" | |
| end | |
| end |
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' | |
| class Embed | |
| include Mongoid::Document | |
| field :_id, type: String | |
| field :f1, type: String | |
| field :f2, type: String | |
| embedded_in :doc | |
| end |
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
| gem 'mongoid', '4.0.0' |
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
| GEM | |
| specs: | |
| activemodel (4.2.0) | |
| activesupport (= 4.2.0) | |
| builder (~> 3.1) | |
| activesupport (4.2.0) | |
| i18n (~> 0.7) | |
| json (~> 1.7, >= 1.7.7) | |
| minitest (~> 5.1) | |
| thread_safe (~> 0.3, >= 0.3.4) | |
| tzinfo (~> 1.1) | |
| bson (2.3.0-java) | |
| builder (3.2.2) | |
| connection_pool (2.1.0) | |
| i18n (0.7.0) | |
| json (1.8.2-java) | |
| minitest (5.5.1) | |
| mongoid (4.0.0) | |
| activemodel (~> 4.0) | |
| moped (~> 2.0.0) | |
| origin (~> 2.1) | |
| tzinfo (>= 0.3.37) | |
| moped (2.0.3) | |
| bson (~> 2.2) | |
| connection_pool (~> 2.0) | |
| optionable (~> 0.2.0) | |
| optionable (0.2.0) | |
| origin (2.1.1) | |
| thread_safe (0.3.4-java) | |
| tzinfo (1.2.2) | |
| thread_safe (~> 0.1) | |
| PLATFORMS | |
| java | |
| DEPENDENCIES | |
| mongoid (= 4.0.0) |
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
| development: | |
| sessions: | |
| default: | |
| database: mongoid_issue_3920 | |
| hosts: | |
| - 127.0.0.1:27017 |
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 'doc' | |
| Mongoid.load!("mongoid.yml", :development) | |
| # Creates simple doc with embedded model | |
| # deletes and updates embedded model by update/(association)_attributes= | |
| # The former method doesn't work with '_destroy' and doesn't trigger 'reject_if', but the last works properly | |
| def create_doc | |
| doc = Doc.new({ | |
| '_id' => 'test', | |
| 'embeds' => [ | |
| { '_id' => 'embed1', 'f1' => 'i\'m here!' }, | |
| { '_id' => 'embed2', 'f2' => 'me too' } | |
| ] | |
| }) | |
| doc.save! | |
| doc | |
| end | |
| def delete_by_update | |
| doc = Doc.find('test') | |
| doc.update({ | |
| 'embeds' => { | |
| "0" => { | |
| '_id' => 'embed1', | |
| '_destroy' => '1' | |
| } | |
| } | |
| }) | |
| doc | |
| end | |
| def delete_by_update_attributes | |
| doc = Doc.find('test') | |
| doc.update({ | |
| 'embeds_attributes' => { | |
| "0" => { | |
| '_id' => 'embed1', | |
| '_destroy' => '1' | |
| } | |
| } | |
| }) | |
| doc | |
| end | |
| def set_by_update | |
| doc = Doc.find('test') | |
| doc.update({ | |
| 'embeds' => { | |
| "0" => { | |
| '_id' => 'embed1', | |
| '_destroy' => '1' | |
| } | |
| } | |
| }) | |
| doc | |
| end | |
| def set_by_update_attributes | |
| doc = Doc.find('test') | |
| doc.update({ | |
| 'embeds_attributes' => { | |
| "0" => { | |
| '_id' => 'embed1', | |
| '_destroy' => '1' | |
| } | |
| } | |
| }) | |
| doc | |
| end | |
| def delete_by_assign | |
| doc = Doc.find('test') | |
| doc.embeds_attributes= ({ | |
| "0" => { | |
| '_id' => 'embed1', | |
| '_destroy' => '1' | |
| }, | |
| }) | |
| doc.save! | |
| doc | |
| end | |
| def set_by_assign | |
| doc = Doc.find('test') | |
| doc.embeds_attributes = { | |
| "0" => { | |
| '_id' => 'embed1', | |
| 'f1' => 'reject' | |
| } | |
| } | |
| doc.save! | |
| doc | |
| end | |
| Doc.delete_all | |
| doc = create_doc | |
| # Uncomment 1 line to test | |
| # doc = delete_by_update # Doesn't delete embedded docs and doesn't trigger 'reject_if' | |
| # doc = delete_by_update_attributes # This works, but not handy for deep nested relations | |
| # doc = set_by_update # Doesn't delete embedded docs and doesn't trigger 'reject_if' | |
| # doc = set_by_update_attributes # This works, but not handy for deep nested relations | |
| # doc = delete_by_assign # Deletes docs properly | |
| # doc = set_by_assign # Triggers 'reject_if' | |
| puts doc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment