What is the mistake in this code for not be able to update an array within a document?
Model
class Foo
include Mongoid::Document
include Mongoid::Timestamps::Created
field :myarray, type: Array
end
Controller
def add_item
@foo = Foo.find_by(uuid: params[:uuid])
unless @foo.nil?
unless @foo.has_attribute? :myarray
@foo[:myarray] = Array.new
end
@foo[:myarray] << params[:item]
@foo.save
end
end
I am using Rails 4 with MongoId 4 and if I do p @foo
before @foo.save
I can see @foo correctly changed but for any reason the update is not persisted.