From 5c350886e2d1d64a6c7bcef164d8c8000a71c2ef Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist Date: Sun, 21 Nov 2010 00:28:06 +0700 Subject: [PATCH] Return and instance of Arel::Attributes when calling `[]` on the model. So, now you can do something like this: Topic.where(Topic[:ratings].gt(10)) Without having to call `#arel_table` first. --- activerecord/lib/active_record/base.rb | 5 +++++ activerecord/test/cases/base_test.rb | 4 ++++ 2 files changed, 9 insertions(+), 0 deletions(-) diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 314f676..3bb02fb 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -837,6 +837,11 @@ module ActiveRecord #:nodoc: end end + # Returns an instance of Arel::Attributes which you can call ARel's method on it. + def [](column_name) + @arel_table[column_name] + end + # Returns a scope for this class without taking into account the default_scope. # # class Post < ActiveRecord::Base diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 26f388c..40011c2 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -92,6 +92,10 @@ class BasicsTest < ActiveRecord::TestCase relation.table.engine = engine end + def test_returning_arel_attribute_instance_using_bracket + assert_equal Topic.arel_table[:id], Topic[:id] + end + def test_preserving_time_objects assert_kind_of( Time, Topic.find(1).bonus_time, -- 1.7.3.1