Skip to content
This repository
Browse code

better error message for missing associations #1631 [courtenay]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1787 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 14762fd229dfdde98bef224f198ff1ade7336b5b 1 parent 1d7aa9f
David Heinemeier Hansson authored
15  activerecord/lib/active_record/associations.rb
@@ -739,13 +739,20 @@ def association_constructor_method(constructor, association_name, association_cl
739 739
         end
740 740
 
741 741
         def find_with_associations(options = {})
742  
-          reflections          = reflect_on_included_associations(options[:include])
  742
+          reflections  = reflect_on_included_associations(options[:include])
  743
+          reflections.each do |r| 
  744
+            raise(
  745
+              NoMethodError, 
  746
+              "Association was not found; perhaps you misspelled it?  You specified :include=>:#{options[:include].join(', :')}"
  747
+            ) if r.nil? 
  748
+          end
  749
+          
743 750
           schema_abbreviations = generate_schema_abbreviations(reflections)
744 751
           primary_key_table    = generate_primary_key_table(reflections, schema_abbreviations)
745 752
 
746  
-          rows        = select_all_rows(options, schema_abbreviations, reflections)
747  
-          records, records_in_order     = { }, []
748  
-          primary_key = primary_key_table[table_name]
  753
+          rows                      = select_all_rows(options, schema_abbreviations, reflections)
  754
+          records, records_in_order = { }, []
  755
+          primary_key               = primary_key_table[table_name]
749 756
           
750 757
           for row in rows
751 758
             id = row[primary_key]
12  activerecord/test/associations_go_eager_test.rb
@@ -92,5 +92,17 @@ def test_eager_with_has_one_dependent_does_not_destroy_dependent
92 92
             :conditions => ["companies.name = ?", "37signals"])
93 93
     assert_not_nil companies(:first_firm, :reload).account
94 94
   end
  95
+
  96
+  def test_eager_with_invalid_association_reference
  97
+    assert_raises(NoMethodError, "Association was not found; perhaps you misspelled it?  You specified :include=>:monkeys") {
  98
+      post = Post.find(6, :include=>[ :monkeys ])
  99
+    }
  100
+    assert_raises(NoMethodError, "Association was not found; perhaps you misspelled it?  You specified :include=>:monkeys, :elephants") {
  101
+      post = Post.find(6, :include=>[ :monkeys, :elephants ])
  102
+    }
  103
+  end
  104
+
95 105
 end
96 106
 
  107
+
  108
+

0 notes on commit 14762fd

Please sign in to comment.
Something went wrong with that request. Please try again.