diff --git a/app/logical/concerns/searchable.rb b/app/logical/concerns/searchable.rb index c1a13a061..f7a9eba0b 100644 --- a/app/logical/concerns/searchable.rb +++ b/app/logical/concerns/searchable.rb @@ -643,7 +643,15 @@ module Searchable end if parameter_hash?(params[attr]) - relation = visible(relation, attr).includes(attr).references(attr).where(attr => model.visible(current_user).search(params[attr], current_user).reorder(nil)) + if association.belongs_to? + foreign_key = association.foreign_key + primary_key = association.association_primary_key + else + foreign_key = association.association_primary_key + primary_key = association.foreign_key + end + + relation = visible(relation, attr).where(foreign_key => model.visible(current_user).search(params[attr], current_user).reorder(nil).select(primary_key)) end relation diff --git a/test/functional/reports_controller_test.rb b/test/functional/reports_controller_test.rb new file mode 100644 index 000000000..2f242947f --- /dev/null +++ b/test/functional/reports_controller_test.rb @@ -0,0 +1,37 @@ +require "test_helper" + +class ReportsControllerTest < ActionDispatch::IntegrationTest + context "The reports controller" do + context "show action" do + context "posts report" do + setup do + @post = create(:post) + end + + should "work" do + get report_path("posts") + + assert_response :success + end + + should "work with the period param" do + get report_path("posts", search: { period: "month" }) + + assert_response :success + end + + should "work with the group param" do + get report_path("posts", search: { group: "uploader" }) + + assert_response :success + end + + should "work when filtering by a nested association" do + get report_path("posts", search: { uploader: { name: @post.uploader.name }}) + + assert_response :success + end + end + end + end +end