diff --git a/app/models/application_record.rb b/app/models/application_record.rb index 6dc792327..cd1ba280a 100644 --- a/app/models/application_record.rb +++ b/app/models/application_record.rb @@ -174,12 +174,8 @@ class ApplicationRecord < ActiveRecord::Base def search(params = {}) params ||= {} - q = all - q = search_attribute(:id, params) - q = search_attribute(:created_at, params) if attribute_names.include?("created_at") - q = search_attribute(:updated_at, params) if attribute_names.include?("updated_at") - - q + default_attributes = (attribute_names & %w[id created_at updated_at]) + search_attributes(params, *default_attributes) end end end diff --git a/test/functional/application_controller_test.rb b/test/functional/application_controller_test.rb index 5ff621fd7..d74a6b815 100644 --- a/test/functional/application_controller_test.rb +++ b/test/functional/application_controller_test.rb @@ -181,4 +181,15 @@ class ApplicationControllerTest < ActionDispatch::IntegrationTest end end end + + context "all index methods" do + should "support searching by the id attribute" do + tags = create_list(:tag, 2, post_count: 42) + get tags_path(format: :json), params: { search: { id: tags.first.id } } + + assert_response :success + assert_equal(1, response.parsed_body.size) + assert_equal(tags.first.id, response.parsed_body.first.fetch("id")) + end + end end