Permalink
Browse files

Give preference to to_a over arel from Relation#method_missing

  • Loading branch information...
lifo committed Jan 19, 2010
1 parent 9465b84 commit dbce07b81d24a991ddd29b91f9c358b1f236bda2
@@ -81,8 +81,8 @@ def update_with_lock(attribute_names = @attributes.keys) #:nodoc:
relation = self.class.unscoped
affected_rows = relation.where(
- relation[self.class.primary_key].eq(quoted_id).and(
- relation[self.class.locking_column].eq(quote_value(previous_value))
+ relation.table[self.class.primary_key].eq(quoted_id).and(
+ relation.table[self.class.locking_column].eq(quote_value(previous_value))
)
).update(arel_attributes_values(false, false, attribute_names))
@@ -8,6 +8,7 @@ class Relation
include FinderMethods, Calculations, SpawnMethods, QueryMethods
delegate :length, :collect, :map, :each, :all?, :include?, :to => :to_a
+ delegate :insert, :update, :where_clause, :to => :arel
attr_reader :table, :klass
@@ -139,10 +140,10 @@ def eager_loading?
protected
def method_missing(method, *args, &block)
- if arel.respond_to?(method)
- arel.send(method, *args, &block)
- elsif Array.method_defined?(method)
+ if Array.method_defined?(method)
to_a.send(method, *args, &block)
+ elsif arel.respond_to?(method)
+ arel.send(method, *args, &block)
elsif match = DynamicFinderMatch.match(method)
attributes = match.attribute_names
super unless @klass.send(:all_attributes_exists?, attributes)
@@ -379,6 +379,12 @@ def test_named_scopes_with_reserved_names
def test_deprecated_named_scope_method
assert_deprecated('named_scope has been deprecated') { Topic.named_scope :deprecated_named_scope }
end
+
+ def test_index_on_named_scope
+ approved = Topic.approved.order('id ASC')
+ assert_equal topics(:second), approved[0]
+ assert approved.loaded?
+ end
end
class DynamicScopeMatchTest < ActiveRecord::TestCase

0 comments on commit dbce07b

Please sign in to comment.