Skip to content

Commit 3738850

Browse files
ryanschkarmi
authored andcommitted
[STORE] Fixed, that MyModel#save does in fact persist updated_at attribute
Without this patch, a persistence model will not actually save the updated_at timestamp to Elasticsearch. Closes elastic#389 Closes elastic#342
1 parent 36bb08e commit 3738850

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

elasticsearch-persistence/lib/elasticsearch/persistence/model.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ def deserialize(document)
121121

122122
# Set up common attributes
123123
#
124-
attribute :created_at, DateTime, default: lambda { |o,a| Time.now.utc }
125-
attribute :updated_at, DateTime, default: lambda { |o,a| Time.now.utc }
124+
attribute :created_at, Time, default: lambda { |o,a| Time.now.utc }
125+
attribute :updated_at, Time, default: lambda { |o,a| Time.now.utc }
126126

127127
attr_reader :hit
128128
end

elasticsearch-persistence/lib/elasticsearch/persistence/model/store.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ def save(options={})
5353
options.update index: self._index if self._index
5454
options.update type: self._type if self._type
5555

56-
response = self.class.gateway.save(self, options)
57-
5856
self[:updated_at] = Time.now.utc
5957

58+
response = self.class.gateway.save(self, options)
59+
6060
@_id = response['_id']
6161
@_index = response['_index']
6262
@_type = response['_type']

elasticsearch-persistence/test/integration/model/model_basic_test.rb

+15-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ class ::Person
135135
person = Person.create name: 'John Smith'
136136
updated_at = person.updated_at
137137

138-
sleep 1
138+
sleep 0.25
139139
person.touch
140140

141141
assert person.updated_at > updated_at, [person.updated_at, updated_at].inspect
@@ -144,6 +144,20 @@ class ::Person
144144
assert found.updated_at > updated_at, [found.updated_at, updated_at].inspect
145145
end
146146

147+
should 'update the object timestamp on save' do
148+
person = Person.create name: 'John Smith'
149+
person.admin = true
150+
sleep 0.25
151+
person.save
152+
153+
Person.gateway.refresh_index!
154+
155+
found = Person.find(person.id)
156+
157+
# Compare without milliseconds
158+
assert_equal person.updated_at.to_i, found.updated_at.to_i
159+
end
160+
147161
should "respect the version" do
148162
person = Person.create name: 'John Smith'
149163

0 commit comments

Comments
 (0)