diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 17a105c397..046dad0653 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -473,6 +473,7 @@ def _select!(*fields) # :nodoc: # .with(posts_with_comments: Post.where("comments_count > ?", 0)) # .with(posts_with_tags: Post.where("tags_count > ?", 0)) def with(*args) + raise ArgumentError, "ActiveRecord::Relation#with does not accept a block" if block_given? check_if_method_has_arguments!(__callee__, args) spawn.with!(*args) end diff --git a/activerecord/test/cases/core_test.rb b/activerecord/test/cases/core_test.rb index feb0fca260..a63e062852 100644 --- a/activerecord/test/cases/core_test.rb +++ b/activerecord/test/cases/core_test.rb @@ -23,7 +23,7 @@ def test_inspect_instance_includes_just_id_by_default end def test_inspect_includes_attributes_from_attributes_for_inspect - Topic.with(attributes_for_inspect: [:id, :title, :author_name]) do + Topic.stub(:attributes_for_inspect, [:id, :title, :author_name]) do topic = topics(:first) assert_equal %(#), topic.inspect @@ -33,7 +33,7 @@ def test_inspect_includes_attributes_from_attributes_for_inspect def test_inspect_instance_with_lambda_date_formatter before = Time::DATE_FORMATS[:inspect] - Topic.with(attributes_for_inspect: [:id, :last_read]) do + Topic.stub(:attributes_for_inspect, [:id, :last_read]) do Time::DATE_FORMATS[:inspect] = ->(date) { "my_format" } topic = topics(:first) @@ -48,7 +48,7 @@ def test_inspect_new_instance end def test_inspect_limited_select_instance - Topic.with(attributes_for_inspect: [:id, :title]) do + Topic.stub(:attributes_for_inspect, [:id, :title]) do assert_equal %(#), Topic.all.merge!(select: "id", where: "id = 1").first.inspect assert_equal %(#), Topic.all.merge!(select: "id, title", where: "id = 1").first.inspect end @@ -64,7 +64,7 @@ def test_inspect_class_without_table end def test_inspect_with_attributes_for_inspect_all_lists_all_attributes - Topic.with(attributes_for_inspect: :all) do + Topic.stub(:attributes_for_inspect, :all) do topic = topics(:first) assert_equal <<~STRING.squish, topic.inspect @@ -108,7 +108,7 @@ def test_pretty_print_persisted end def test_pretty_print_full - Topic.with(attributes_for_inspect: :all) do + Topic.stub(:attributes_for_inspect, :all) do topic = topics(:first) actual = +"" PP.pp(topic, StringIO.new(actual)) diff --git a/activerecord/test/cases/integration_test.rb b/activerecord/test/cases/integration_test.rb index 31c04f53a8..87e6ac6016 100644 --- a/activerecord/test/cases/integration_test.rb +++ b/activerecord/test/cases/integration_test.rb @@ -103,14 +103,14 @@ def test_to_param_for_a_composite_primary_key_model end def test_param_delimiter_changes_delimiter_used_in_to_param - Cpk::Order.with(param_delimiter: ",") do + Cpk::Order.stub(:param_delimiter, ",") do assert_equal("1,123", Cpk::Order.new(id: [1, 123]).to_param) end end def test_param_delimiter_is_defined_per_class - Cpk::Order.with(param_delimiter: ",") do - Cpk::Book.with(param_delimiter: ";") do + Cpk::Order.stub(:param_delimiter, ",") do + Cpk::Book.stub(:param_delimiter, ";") do assert_equal("1,123", Cpk::Order.new(id: [1, 123]).to_param) assert_equal("1;123", Cpk::Book.new(id: [1, 123]).to_param) end diff --git a/activerecord/test/cases/relation/with_test.rb b/activerecord/test/cases/relation/with_test.rb index f6eccee3ed..a34dd5dbc0 100644 --- a/activerecord/test/cases/relation/with_test.rb +++ b/activerecord/test/cases/relation/with_test.rb @@ -82,6 +82,12 @@ def test_with_left_joins assert_equal Post.count, records.size assert_equal POSTS_WITH_COMMENTS, records.filter_map { _1.id if _1.has_comments } end + + def test_raises_when_using_block + assert_raises(ArgumentError, match: "does not accept a block") do + Post.with(attributes_for_inspect: :id) { } + end + end else def test_common_table_expressions_are_unsupported assert_raises ActiveRecord::StatementInvalid do