aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2016-06-05 00:38:40 +0900
committerRyuta Kamizono <kamipo@gmail.com>2016-10-10 11:41:07 +0900
commit0da4a08bdfea28a0cc881ca5831aadfcf8a3b7eb (patch)
tree7cb7b9c9bc5a9efb29a19a5a05a0bb02c6a9d50d /activerecord/test
parent88e00d303df5caad9393f81a12a1eaf227b2e293 (diff)
downloadrails-0da4a08bdfea28a0cc881ca5831aadfcf8a3b7eb.tar.gz
rails-0da4a08bdfea28a0cc881ca5831aadfcf8a3b7eb.tar.bz2
rails-0da4a08bdfea28a0cc881ca5831aadfcf8a3b7eb.zip
Prevent `RangeError` for `belongs_to` associations
Currently to access `belongs_to` associations raises a `RangeError` if foreign key attribute has out of range value. It should return a nil value rather than raising a `RangeError`. Fixes #20140.
Diffstat (limited to 'activerecord/test')
-rw-r--r--activerecord/test/cases/associations/belongs_to_associations_test.rb14
1 files changed, 14 insertions, 0 deletions
diff --git a/activerecord/test/cases/associations/belongs_to_associations_test.rb b/activerecord/test/cases/associations/belongs_to_associations_test.rb
index 2418346d1b..da6e42b89b 100644
--- a/activerecord/test/cases/associations/belongs_to_associations_test.rb
+++ b/activerecord/test/cases/associations/belongs_to_associations_test.rb
@@ -1062,6 +1062,20 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase
assert_equal 1, parent.reload.children_count
end
+ def test_belongs_to_with_out_of_range_value_assigning
+ model = Class.new(Comment) do
+ def self.name; "Temp"; end
+ validates :post, presence: true
+ end
+
+ comment = model.new
+ comment.post_id = 10_000_000_000
+
+ assert_nil comment.post
+ assert_not comment.valid?
+ assert_equal [{ error: :blank }], comment.errors.details[:post]
+ end
+
def test_polymorphic_with_custom_primary_key
toy = Toy.create!
sponsor = Sponsor.create!(sponsorable: toy)