Mik-die/mongoid_globalize — Issue #2: Cant Modify Frozen Hash On Delete
Author: dbkbali
Created: 2011-10-12 09:22:56+00:00
State: open

I have the following model:

class Activity
include Mongoid::Document
include Mongoid::Slug
include Mongoid::Globalize

references_and_referenced_in_many :categories, :inverse_of => :activities, :dependent => :nullify, :validate => false

translates do
field :name
fallbacks_for_empty_translations!
end

slug :name, :permanent => true do |doc|
"#{doc.translated_attributes['name']}"
end

accepts_nested_attributes_for :categories, :category_ids
accepts_nested_attributes_for :translations, :allow_destroy => true, :reject_if => proc {|rec| rec['name'].blank?}

attr_accessible :name, :categories, :category_ids, :translations

validates :name, default_locale: true
validates :name, unique_in_locale: true

end

Using Mongoid 2.3.1 and Rails 3.1.1 in console:

ruby-1.9.2-p290-patched :001 > act = Activity.create(:name => "exercise")
=> #
ruby-1.9.2-p290-patched :002 > act.translations
=> [#]
ruby-1.9.2-p290-patched :003 > act.reload
=> #
ruby-1.9.2-p290-patched :004 > act.delete
RuntimeError: can't modify frozen hash
from /Users/dbk/Development/mongoid_globalize/lib/mongoid_globalize/instance_methods.rb:18:in `merge!'
from /Users/dbk/Development/mongoid_globalize/lib/mongoid_globalize/instance_methods.rb:18:in `attributes'
from /Users/dbk/.rvm/gems/ruby-1.9.2-p290-patched@rails31/gems/mongoid-2.3.1/lib/mongoid/attributes.rb:46:in `read_attribute'
from /Users/dbk/Development/mongoid_globalize/lib/mongoid_globalize/instance_methods.rb:64:in `read_attribute'
from /Users/dbk/.rvm/gems/ruby-1.9.2-p290-patched@rails31/gems/mongoid-2.3.1/lib/mongoid/fields.rb:300:in `block (2 levels) in create_accessors'
from /Users/dbk/.rvm/gems/ruby-1.9.2-p290-patched@rails31/gems/mongoid-2.3.1/lib/mongoid/identity_map.rb:34:in `remove'
from /Users/dbk/.rvm/gems/ruby-1.9.2-p290-patched@rails31/gems/mongoid-2.3.1/lib/mongoid/identity_map.rb:116:in `remove'
from /Users/dbk/.rvm/gems/ruby-1.9.2-p290-patched@rails31/gems/mongoid-2.3.1/lib/mongoid/persistence/deletion.rb:26:in `prepare'
from /Users/dbk/.rvm/gems/ruby-1.9.2-p290-patched@rails31/gems/mongoid-2.3.1/lib/mongoid/persistence/operations/remove.rb:26:in `persist'
from /Users/dbk/.rvm/gems/ruby-1.9.2-p290-patched@rails31/gems/mongoid-2.3.1/lib/mongoid/persistence.rb:56:in `remove'
from (irb):4
from /Users/dbk/.rvm/gems/ruby-1.9.2-p290-patched@rails31/gems/railties-3.1.1/lib/rails/commands/console.rb:45:in `start'
from /Users/dbk/.rvm/gems/ruby-1.9.2-p290-patched@rails31/gems/railties-3.1.1/lib/rails/commands/console.rb:8:in `start'
from /Users/dbk/.rvm/gems/ruby-1.9.2-p290-patched@rails31/gems/railties-3.1.1/lib/rails/commands.rb:40:in `'
from script/rails:6:in `require'
from script/rails:6:in `

'
ruby-1.9.2-p290-patched :005 >

I upgraded your gem to use mongoid 2.3.1 and wrote a delete test all of these tests pass, I also tried without mongoid slug and I am still getting the same error. I am still trying to debug this but if you have any ideas as to what might be causing this it would be helpful. I note that this is crashing in the method you indicated as the most "trouble".