Fix #5001: Media assets not searchable through upload records.
Fix this: https://danbooru.donmai.us/uploads.json?search[media_assets][md5]=b83daa7f1ae7e4127b1befd32f71ba10 failing with an ActiveRecord::StatementInvalid error. The bug was that for a `has_many through: ...` association, like `has_many :media_assets, through: :upload_media_assets`, we weren't joining on the associated table properly so we ended up generating invalid SQL.
This commit is contained in:
@@ -436,7 +436,7 @@ module Searchable
|
||||
end
|
||||
|
||||
if parameter_hash?(params[attr])
|
||||
relation = relation.where(attr => model.visible(current_user).search(params[attr]).reorder(nil))
|
||||
relation = relation.includes(attr).references(attr).where(attr => model.visible(current_user).search(params[attr]).reorder(nil))
|
||||
end
|
||||
|
||||
relation
|
||||
|
||||
@@ -3,4 +3,9 @@
|
||||
class UploadMediaAsset < ApplicationRecord
|
||||
belongs_to :upload
|
||||
belongs_to :media_asset
|
||||
|
||||
def self.search(params)
|
||||
q = search_attributes(params, :id, :created_at, :updated_at, :upload, :media_asset)
|
||||
q.apply_default_order(params)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -252,5 +252,17 @@ class SearchableTest < ActiveSupport::TestCase
|
||||
assert_search_equals(@p1, has_comments: true, comments: { id: @p1.comments.first.id })
|
||||
end
|
||||
end
|
||||
|
||||
context "for a `has_many through: ...` association" do
|
||||
subject { Upload }
|
||||
|
||||
should "work" do
|
||||
@media_asset = create(:media_asset)
|
||||
@upload1 = create(:upload, media_assets: [@media_asset])
|
||||
@upload2 = create(:upload, media_assets: [@media_asset])
|
||||
|
||||
assert_search_equals([@upload2, @upload1], media_asset: { md5: @media_asset.md5 })
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user