Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #12194 from thedarkone/readonly-merger-fix

Relation#merge should not lose readonly(false) flag.
  • Loading branch information...
commit 2a0f2603d18064a1997b5c31a07b0e17edb1fbf5 2 parents 8028768 + 5cbed2b
@rafaelfranca rafaelfranca authored
View
4 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*
View
6 activerecord/lib/active_record/relation/merger.rb
@@ -58,7 +58,11 @@ def normal_values
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
View
8 activerecord/test/cases/relation_test.rb
@@ -193,6 +193,14 @@ def self.sanitize_sql(args)
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)
Please sign in to comment.
Something went wrong with that request. Please try again.