Skip to content

Commit

Permalink
Improve clarity of error message for missing includes and eager_load
Browse files Browse the repository at this point in the history
relations
  • Loading branch information
RochesterinNYC committed Jan 28, 2016
1 parent 1ac3fb9 commit 867a9cb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
Expand Up @@ -228,7 +228,7 @@ def walk(left, right)

def find_reflection(klass, name)
klass._reflect_on_association(name) or
raise ConfigurationError, "Association named '#{ name }' was not found on #{ klass.name }; perhaps you misspelled it?"
raise ConfigurationError, "Can't join '#{ klass.name }' to association named '#{ name }'; perhaps you misspelled it?"
end

def build(associations, base_klass)
Expand Down
17 changes: 17 additions & 0 deletions activerecord/test/cases/associations/join_model_test.rb
Expand Up @@ -740,6 +740,23 @@ def test_has_many_with_pluralize_table_names_false
assert_equal aircraft.engines, [engine]
end

def test_proper_error_message_for_eager_load_and_includes_association_errors
includes_error = assert_raises(ActiveRecord::ConfigurationError) {
Post.includes(:nonexistent_relation).where(nonexistent_relation: {name: 'Rochester'}).find(1)
}
assert_equal("Can't join 'Post' to association named 'nonexistent_relation'; perhaps you misspelled it?", includes_error.message)

eager_load_error = assert_raises(ActiveRecord::ConfigurationError) {
Post.eager_load(:nonexistent_relation).where(nonexistent_relation: {name: 'Rochester'}).find(1)
}
assert_equal("Can't join 'Post' to association named 'nonexistent_relation'; perhaps you misspelled it?", eager_load_error.message)

includes_and_eager_load_error = assert_raises(ActiveRecord::ConfigurationError) {
Post.eager_load(:nonexistent_relation).includes(:nonexistent_relation).where(nonexistent_relation: {name: 'Rochester'}).find(1)
}
assert_equal("Can't join 'Post' to association named 'nonexistent_relation'; perhaps you misspelled it?", includes_and_eager_load_error.message)
end

private
# create dynamic Post models to allow different dependency options
def find_post_with_dependency(post_id, association, association_name, dependency)
Expand Down

0 comments on commit 867a9cb

Please sign in to comment.