aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2013-09-11 12:18:57 -0700
committerRafael Mendonça França <rafaelmfranca@gmail.com>2013-09-11 12:18:57 -0700
commit2a0f2603d18064a1997b5c31a07b0e17edb1fbf5 (patch)
tree09a618fc4c80e5dff3215b81e7ede5144a8b52dc /activerecord
parent802876854d23e7735c8647e0a7b90440db095d8a (diff)
parent5cbed2bc606750c71ee231d2b816471fe241459e (diff)
downloadrails-2a0f2603d18064a1997b5c31a07b0e17edb1fbf5.tar.gz
rails-2a0f2603d18064a1997b5c31a07b0e17edb1fbf5.tar.bz2
rails-2a0f2603d18064a1997b5c31a07b0e17edb1fbf5.zip
Merge pull request #12194 from thedarkone/readonly-merger-fix
Relation#merge should not lose readonly(false) flag.
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG.md4
-rw-r--r--activerecord/lib/active_record/relation/merger.rb6
-rw-r--r--activerecord/test/cases/relation_test.rb8
3 files changed, 17 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index 9041f9d33b..d2f2c745a0 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,3 +1,7 @@
+* Fix `AR::Relation#merge` sometimes failing to preserve `readonly(false)` flag.
+
+ *thedarkone*
+
* Re-use `order` argument pre-processing for `reorder`.
*Paul Nikitochkin*
diff --git a/activerecord/lib/active_record/relation/merger.rb b/activerecord/lib/active_record/relation/merger.rb
index 530c47d0d0..c05632e688 100644
--- a/activerecord/lib/active_record/relation/merger.rb
+++ b/activerecord/lib/active_record/relation/merger.rb
@@ -58,7 +58,11 @@ module ActiveRecord
def merge
normal_values.each do |name|
value = values[name]
- relation.send("#{name}!", *value) unless value.blank?
+ # The unless clause is here mostly for performance reasons (since the `send` call might be moderately
+ # expensive), most of the time the value is going to be `nil` or `.blank?`, the only catch is that
+ # `false.blank?` returns `true`, so there needs to be an extra check so that explicit `false` values
+ # don't fall through the cracks.
+ relation.send("#{name}!", *value) unless value.nil? || (value.blank? && false != value)
end
merge_multi_values
diff --git a/activerecord/test/cases/relation_test.rb b/activerecord/test/cases/relation_test.rb
index a327b0d3e5..e77de19ac3 100644
--- a/activerecord/test/cases/relation_test.rb
+++ b/activerecord/test/cases/relation_test.rb
@@ -193,6 +193,14 @@ module ActiveRecord
assert_equal ['foo = bar'], relation.where_values
end
+ def test_merging_readonly_false
+ relation = Relation.new FakeKlass, :b
+ readonly_false_relation = relation.readonly(false)
+ # test merging in both directions
+ assert_equal false, relation.merge(readonly_false_relation).readonly_value
+ assert_equal false, readonly_false_relation.merge(relation).readonly_value
+ end
+
def test_relation_merging_with_merged_joins_as_symbols
special_comments_with_ratings = SpecialComment.joins(:ratings)
posts_with_special_comments_with_ratings = Post.group("posts.id").joins(:special_comments).merge(special_comments_with_ratings)