From dcae5ab8e276f8ab3c6328e0229ff6d778a99864 Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Thu, 10 Jun 2010 10:55:39 -0400 Subject: [PATCH] ensure that ActiveRecord::Relation does not blow up on to_json --- activerecord/lib/active_record/relation.rb | 4 ++++ activerecord/test/cases/relations_test.rb | 5 +++++ 2 files changed, 9 insertions(+), 0 deletions(-) diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index 99c914d..e461b3a 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -28,6 +28,10 @@ module ActiveRecord apply_modules(Module.new(&block)) if block_given? end + def as_json(options=nil) + self.to_a + end + def new(*args, &block) with_create_scope { @klass.new(*args, &block) } end diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index 43519db..3015c45 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -22,6 +22,11 @@ class RelationTest < ActiveRecord::TestCase assert_equal 4, topics.size end + def test_to_json + assert_nothing_raised { Bird.scoped.to_json } + assert_nothing_raised { Bird.scoped.all.to_json } + end + def test_scoped_all topics = Topic.scoped.all assert_kind_of Array, topics -- 1.6.5.2