Fix searching models by id/created_at/updated_at attributes.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user