diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2005-12-08 04:44:54 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2005-12-08 04:44:54 +0000 |
commit | e466fc45cade7cbc0ee5a0e7181e377dd3780043 (patch) | |
tree | 31402d4103e4015a1ce4f99b5fd73628447cd121 | |
parent | c140e80fc4ca438cd966ec3b2453cb7bef91482b (diff) | |
download | rails-e466fc45cade7cbc0ee5a0e7181e377dd3780043.tar.gz rails-e466fc45cade7cbc0ee5a0e7181e377dd3780043.tar.bz2 rails-e466fc45cade7cbc0ee5a0e7181e377dd3780043.zip |
Fixed that using :include together with :conditions array in Base.find would cause NoMethodError (closes #2887) [Paul Hammmond]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3240 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r-- | activerecord/CHANGELOG | 2 | ||||
-rwxr-xr-x | activerecord/lib/active_record/associations.rb | 7 | ||||
-rw-r--r-- | activerecord/test/associations_go_eager_test.rb | 24 |
3 files changed, 30 insertions, 3 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 7fe0f3ae9b..e4ab429e6a 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Fixed that using :include together with :conditions array in Base.find would cause NoMethodError #2887 [Paul Hammmond] + * PostgreSQL: more robust sequence name discovery. #3087 [Rick Olson] * Oracle: use syntax compatible with Oracle 8. #3131 [Michael Schoen] diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 2fcce6348e..c50750352f 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1029,9 +1029,10 @@ module ActiveRecord end def include_eager_conditions?(options) - return false unless options[:conditions] - - options[:conditions].scan(/ ([^.]+)\.[^.]+ /).flatten.any? do |condition_table_name| + conditions = options[:conditions] + return false unless conditions + conditions = conditions.first if conditions.is_a?(Array) + conditions.scan(/(\w+)\.\w+/).flatten.any? do |condition_table_name| condition_table_name != table_name end end diff --git a/activerecord/test/associations_go_eager_test.rb b/activerecord/test/associations_go_eager_test.rb index 26dc4456df..4e9c006d84 100644 --- a/activerecord/test/associations_go_eager_test.rb +++ b/activerecord/test/associations_go_eager_test.rb @@ -83,6 +83,12 @@ class EagerAssociationTest < Test::Unit::TestCase assert_equal [6,7,8], comments.collect { |c| c.id } end + def test_eager_association_loading_with_belongs_to_and_limit_and_offset_and_conditions_array + comments = Comment.find(:all, :include => :post, :conditions => ['post_id = ?',4], :limit => 3, :offset => 1, :order => 'comments.id') + assert_equal 3, comments.length + assert_equal [6,7,8], comments.collect { |c| c.id } + end + def test_eager_association_loading_with_belongs_to_and_limit_and_multiple_associations posts = Post.find(:all, :include => [:author, :very_special_comment], :limit => 1) assert_equal 1, posts.length @@ -101,6 +107,24 @@ class EagerAssociationTest < Test::Unit::TestCase assert_equal 3, posts.inject(0) { |sum, post| sum += post.comments.size } end + def test_eager_with_has_many_and_limit_and_conditions + posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => "posts.body = 'hello'", :order => "posts.id") + assert_equal 2, posts.size + assert_equal [4,5], posts.collect { |p| p.id } + end + + def test_eager_with_has_many_and_limit_and_conditions_array + posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => [ "posts.body = ?", 'hello' ], :order => "posts.id") + assert_equal 2, posts.size + assert_equal [4,5], posts.collect { |p| p.id } + end + + def test_eager_with_has_many_and_limit_and_conditions_array_on_the_eagers + assert_raises(ArgumentError) do + posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => [ "authors.name = ?", 'David' ]) + end + end + def test_eager_with_has_many_and_limit_with_no_results posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => "posts.title = 'magic forest'") assert_equal 0, posts.size |