Details
-
Improvement
-
Status: Closed
-
Major - P3
-
Resolution: Duplicate
-
None
-
None
-
None
-
4
Description
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.
Otherwise if I change {{ @foo[:myarray] << params[:item] }} by {{ @foo[:myarray] += [ params[:item] ] }} then all works fine.
I'm missing something in the first way?
Attachments
Issue Links
- duplicates
-
MONGOID-2951 Attributes of container types are not persisted when mutated
-
- Backlog
-