Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Relations: Added offset when finding with associations. Delegate arra…

…y instance

methods to to_a.
  • Loading branch information...
commit 9a958a3d7dc1640c90abbe1164d7ccb7ea9ae685 1 parent aadb09b
@miloops miloops authored
View
8 activerecord/lib/active_record/relation.rb
@@ -1,6 +1,7 @@
module ActiveRecord
class Relation
delegate :to_sql, :to => :relation
+ delegate :length, :collect, :find, :map, :each, :to => :to_a
attr_reader :relation, :klass
def initialize(klass, relation)
@@ -34,7 +35,8 @@ def to_a
:group => @relation.send(:group_clauses).join(', '),
:order => @relation.send(:order_clauses).join(', '),
:conditions => @relation.send(:where_clauses).join("\n\tAND "),
- :limit => @relation.taken
+ :limit => @relation.taken,
+ :offset => @relation.skipped
},
ActiveRecord::Associations::ClassMethods::JoinDependency.new(@klass, @eager_load_associations, nil))
end
@@ -49,10 +51,6 @@ def to_a
records
end
- def each(&block)
- to_a.each(&block)
- end
-
def first
@relation = @relation.take(1)
to_a.first
View
9 activerecord/test/cases/relations_test.rb
@@ -122,12 +122,12 @@ def test_default_scope_with_conditions_string
end
def test_default_scope_with_conditions_hash
- assert_equal Developer.find_all_by_name('Jamis').map(&:id).sort, DeveloperCalledJamis.all.to_a.map(&:id).sort
+ assert_equal Developer.find_all_by_name('Jamis').map(&:id).sort, DeveloperCalledJamis.all.map(&:id).sort
assert_equal 'Jamis', DeveloperCalledJamis.create!.name
end
def test_loading_with_one_association
- posts = Post.all(:include => :comments).to_a
+ posts = Post.all(:include => :comments)
post = posts.find { |p| p.id == 1 }
assert_equal 2, post.comments.size
assert post.comments.include?(comments(:greetings))
@@ -136,16 +136,15 @@ def test_loading_with_one_association
assert_equal 2, post.comments.size
assert post.comments.include?(comments(:greetings))
- posts = Post.all(:include => :last_comment).to_a
+ posts = Post.all(:include => :last_comment)
post = posts.find { |p| p.id == 1 }
assert_equal Post.find(1).last_comment, post.last_comment
end
def test_loading_with_one_association_with_non_preload
- posts = Post.all(:include => :last_comment, :order => 'comments.id DESC').to_a
+ posts = Post.all(:include => :last_comment, :order => 'comments.id DESC')
post = posts.find { |p| p.id == 1 }
assert_equal Post.find(1).last_comment, post.last_comment
end
-
end
Please sign in to comment.
Something went wrong with that request. Please try again.