2

Readonly in field of type Hash in MongoId not work property

class Model
  include Mongoid::Document

  field :data, type: Hash

  attr_readonly :data
end

This code is ok:

Model.create!(data: { foo: 'bar' })
puts Model.last.data # => { foo: 'bar' }
Model.update!(data: { baz: 'bar' }) # => raise exception

But when edit the hash directly the readonly not work:

model_instance = Model.last
model_instance.data # => { foo: 'bar' }

model_instance.data[:baz] = 'bar'
model_instance.update!
model_instance.reload
model_instance.data # => { foo: 'bar', baz: 'bar' } # problem, the values are saves in db.

who i could put readonly in hash fields?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.