Show proper error message when a non-relation object is passed to AR::Relation#or

- Previously it used to show error message
    <"undefined method `limit_value' for {:title=>\"Rails\"}:Hash">

- Now it shows following error message.

    >> Post.where.not(name: 'DHH').or(name: 'Tenderlove')
    ArgumentError: You have passed Hash object to #or. Pass an ActiveRecord::Relation object instead.

- Fixes #23714.
This commit is contained in:
Prathamesh Sonpatki 2016-02-17 11:47:37 +05:30
parent bb4a85da10
commit e254665ac0
2 changed files with 10 additions and 0 deletions

@ -655,6 +655,10 @@ def rewhere(conditions)
# # SELECT `posts`.* FROM `posts` WHERE (('id = 1' OR 'author_id = 3'))
#
def or(other)
unless other.is_a? Relation
raise ArgumentError, "You have passed #{other.class.name} object to #or. Pass an ActiveRecord::Relation object instead."
end
spawn.or!(other)
end

@ -82,5 +82,11 @@ def test_or_on_loaded_relation
assert_equal p.loaded?, true
assert_equal expected, p.or(Post.where('id = 2')).to_a
end
def test_or_with_non_relation_object_raises_error
assert_raises ArgumentError do
Post.where(id: [1, 2, 3]).or(title: 'Rails')
end
end
end
end