diff options
author | Matthew Draper <matthew@trebex.net> | 2016-01-13 15:57:42 +1030 |
---|---|---|
committer | Matthew Draper <matthew@trebex.net> | 2016-01-13 15:57:42 +1030 |
commit | ffdeb3a62a9b2fff95c2f5f03b0e31549fb21118 (patch) | |
tree | 1a78e4dedffa03f58a802811b070e32e2d0e4532 /activerecord | |
parent | 7b7b12f5983fd0c48e4a8dad909d3388b72f046c (diff) | |
parent | f466cd7fc466ad7b602508b255e1de41074c7df8 (diff) | |
download | rails-ffdeb3a62a9b2fff95c2f5f03b0e31549fb21118.tar.gz rails-ffdeb3a62a9b2fff95c2f5f03b0e31549fb21118.tar.bz2 rails-ffdeb3a62a9b2fff95c2f5f03b0e31549fb21118.zip |
Merge pull request #23040 from rafaelfranca/or-error-message
Improve error message for #or when it is structurally incompatible
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/relation/query_methods.rb | 14 | ||||
-rw-r--r-- | activerecord/test/cases/relation/or_test.rb | 4 |
2 files changed, 11 insertions, 7 deletions
diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 5635b4215b..716b1e8505 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -659,8 +659,10 @@ module ActiveRecord end def or!(other) # :nodoc: - unless structurally_compatible_for_or?(other) - raise ArgumentError, 'Relation passed to #or must be structurally compatible' + incompatible_values = structurally_incompatible_values_for_or(other) + + unless incompatible_values.empty? + raise ArgumentError, "Relation passed to #or must be structurally compatible. Incompatible values: #{incompatible_values}" end self.where_clause = self.where_clause.or(other.where_clause) @@ -1189,10 +1191,10 @@ module ActiveRecord end end - def structurally_compatible_for_or?(other) - Relation::SINGLE_VALUE_METHODS.all? { |m| send("#{m}_value") == other.send("#{m}_value") } && - (Relation::MULTI_VALUE_METHODS - [:extending]).all? { |m| send("#{m}_values") == other.send("#{m}_values") } && - (Relation::CLAUSE_METHODS - [:having, :where]).all? { |m| send("#{m}_clause") == other.send("#{m}_clause") } + def structurally_incompatible_values_for_or(other) + Relation::SINGLE_VALUE_METHODS.reject { |m| send("#{m}_value") == other.send("#{m}_value") } + + (Relation::MULTI_VALUE_METHODS - [:extending]).reject { |m| send("#{m}_values") == other.send("#{m}_values") } + + (Relation::CLAUSE_METHODS - [:having, :where]).reject { |m| send("#{m}_clause") == other.send("#{m}_clause") } end def new_where_clause diff --git a/activerecord/test/cases/relation/or_test.rb b/activerecord/test/cases/relation/or_test.rb index 2006fc9611..28a0862f91 100644 --- a/activerecord/test/cases/relation/or_test.rb +++ b/activerecord/test/cases/relation/or_test.rb @@ -52,9 +52,11 @@ module ActiveRecord end def test_or_with_incompatible_relations - assert_raises ArgumentError do + error = assert_raises ArgumentError do Post.order('body asc').where('id = 1').or(Post.order('id desc').where(:id => [2, 3])).to_a end + + assert_equal "Relation passed to #or must be structurally compatible. Incompatible values: [:order]", error.message end def test_or_when_grouping |