Skip to content

Commit ee0af92

Browse files
trebykarmi
authored andcommitted
[MODEL] Fixed a bug where continuous #save calls emptied the @__changed_attributes variable
Closes elastic#339
1 parent b1d44fb commit ee0af92

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

elasticsearch-model/lib/elasticsearch/model/proxy.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@ def __elasticsearch__ &block
5959
# @see http://api.rubyonrails.org/classes/ActiveModel/Dirty.html
6060
#
6161
before_save do |i|
62+
changed_attr = i.__elasticsearch__.instance_variable_get(:@__changed_attributes) || {}
6263
i.__elasticsearch__.instance_variable_set(:@__changed_attributes,
63-
Hash[ i.changes.map { |key, value| [key, value.last] } ])
64+
changed_attr.merge(Hash[ i.changes.map { |key, value| [key, value.last] } ]))
6465
end if respond_to?(:before_save) && instance_methods.include?(:changed_attributes)
6566
end
6667
end

elasticsearch-model/test/integration/active_record_basic_test.rb

+28-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class ActiveRecordBasicIntegrationTest < Elasticsearch::Test::IntegrationTestCas
1111
ActiveRecord::Schema.define(:version => 1) do
1212
create_table :articles do |t|
1313
t.string :title
14+
t.string :body
1415
t.datetime :created_at, :default => 'NOW()'
1516
end
1617
end
@@ -22,6 +23,7 @@ class ::Article < ActiveRecord::Base
2223
settings index: { number_of_shards: 1, number_of_replicas: 0 } do
2324
mapping do
2425
indexes :title, type: 'string', analyzer: 'snowball'
26+
indexes :body, type: 'string'
2527
indexes :created_at, type: 'date'
2628
end
2729
end
@@ -30,9 +32,9 @@ class ::Article < ActiveRecord::Base
3032
Article.delete_all
3133
Article.__elasticsearch__.create_index! force: true
3234

33-
::Article.create! title: 'Test'
34-
::Article.create! title: 'Testing Coding'
35-
::Article.create! title: 'Coding'
35+
::Article.create! title: 'Test', body: ''
36+
::Article.create! title: 'Testing Coding', body: ''
37+
::Article.create! title: 'Coding', body: ''
3638

3739
Article.__elasticsearch__.refresh_index!
3840
end
@@ -146,6 +148,29 @@ class ::Article < ActiveRecord::Base
146148
assert_equal 1, response.records.size
147149
end
148150

151+
should "update document when save is called multiple times in a transaction" do
152+
article = Article.first
153+
response = Article.search 'body:dummy'
154+
155+
assert_equal 0, response.results.size
156+
assert_equal 0, response.records.size
157+
158+
ActiveRecord::Base.transaction do
159+
article.body = 'dummy'
160+
article.save
161+
162+
article.title = 'special'
163+
article.save
164+
end
165+
166+
article.__elasticsearch__.update_document
167+
Article.__elasticsearch__.refresh_index!
168+
169+
response = Article.search 'body:dummy'
170+
assert_equal 1, response.results.size
171+
assert_equal 1, response.records.size
172+
end
173+
149174
should "return results for a DSL search" do
150175
response = Article.search query: { match: { title: { query: 'test' } } }
151176

0 commit comments

Comments
 (0)