Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better error messages when association name is invalid in the argument of ActiveRecord::QueryMethods::WhereChain#missing #44000

Merged
merged 1 commit into from Jan 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions activerecord/lib/active_record/relation/query_methods.rb
Expand Up @@ -97,6 +97,9 @@ def associated(*associations)
def missing(*associations)
associations.each do |association|
reflection = @scope.klass._reflect_on_association(association)
unless reflection
raise ArgumentError.new("An association named `:#{association}` does not exist on the model `#{@scope.name}`.")
end
@scope.left_outer_joins!(association)
@scope.where!(reflection.table_name => { reflection.association_primary_key => nil })
end
Expand Down
8 changes: 8 additions & 0 deletions activerecord/test/cases/relation/where_chain_test.rb
Expand Up @@ -33,6 +33,14 @@ def test_missing_with_association
assert_equal [posts(:authorless)], Post.where.missing(:author).to_a
end

def test_missing_with_invalid_association_name
e = assert_raises(ArgumentError) do
Post.where.missing(:cars).to_a
end

assert_match(/An association named `:cars` does not exist on the model `Post`\./, e.message)
end

def test_missing_with_multiple_association
assert posts(:authorless).comments.empty?
assert_equal [posts(:authorless)], Post.where.missing(:author, :comments).to_a
Expand Down