Skip to content

Commit

Permalink
better error message for missing associations rails#1631 [courtenay]
Browse files Browse the repository at this point in the history
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1787 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
dhh committed Jul 9, 2005
1 parent 1d7aa9f commit 14762fd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
15 changes: 11 additions & 4 deletions activerecord/lib/active_record/associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -739,13 +739,20 @@ def association_constructor_method(constructor, association_name, association_cl
end

def find_with_associations(options = {})
reflections = reflect_on_included_associations(options[:include])
reflections = reflect_on_included_associations(options[:include])
reflections.each do |r|
raise(
NoMethodError,
"Association was not found; perhaps you misspelled it? You specified :include=>:#{options[:include].join(', :')}"
) if r.nil?
end

schema_abbreviations = generate_schema_abbreviations(reflections)
primary_key_table = generate_primary_key_table(reflections, schema_abbreviations)

rows = select_all_rows(options, schema_abbreviations, reflections)
records, records_in_order = { }, []
primary_key = primary_key_table[table_name]
rows = select_all_rows(options, schema_abbreviations, reflections)
records, records_in_order = { }, []
primary_key = primary_key_table[table_name]

for row in rows
id = row[primary_key]
Expand Down
12 changes: 12 additions & 0 deletions activerecord/test/associations_go_eager_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,17 @@ def test_eager_with_has_one_dependent_does_not_destroy_dependent
:conditions => ["companies.name = ?", "37signals"])
assert_not_nil companies(:first_firm, :reload).account
end

def test_eager_with_invalid_association_reference
assert_raises(NoMethodError, "Association was not found; perhaps you misspelled it? You specified :include=>:monkeys") {
post = Post.find(6, :include=>[ :monkeys ])
}
assert_raises(NoMethodError, "Association was not found; perhaps you misspelled it? You specified :include=>:monkeys, :elephants") {
post = Post.find(6, :include=>[ :monkeys, :elephants ])
}
end

end



0 comments on commit 14762fd

Please sign in to comment.