diff --git a/audiences/app/models/audiences/users_search.rb b/audiences/app/models/audiences/users_search.rb index edce4c82..efd8a3e8 100644 --- a/audiences/app/models/audiences/users_search.rb +++ b/audiences/app/models/audiences/users_search.rb @@ -27,7 +27,14 @@ def users private def result - @result ||= @scope.where("data LIKE ?", "%#{@query}%") + @result ||= @scope.where("#{data_attribute_query} LIKE ?", "%#{@query}%") + end + + def data_attribute_query + case @scope.connection.adapter_name + when "PostgreSQL" then "to_json(data)" + else "CAST(data as CHAR)" + end end end end diff --git a/audiences/spec/models/audiences/user_search_spec.rb b/audiences/spec/models/audiences/user_search_spec.rb index 3f68895f..369dac80 100644 --- a/audiences/spec/models/audiences/user_search_spec.rb +++ b/audiences/spec/models/audiences/user_search_spec.rb @@ -4,8 +4,14 @@ RSpec.describe Audiences::UsersSearch do it "searches through any serialized data attribute" do - john_doe = Audiences::ExternalUser.create(user_id: 123, data: { name: "John Doe" }) - frank_doe = Audiences::ExternalUser.create(user_id: 321, data: { name: "Frank Doe", territory: "Philadelphia" }) + john_doe = Audiences::ExternalUser.create( + user_id: 123, + data: { displayName: "John Doe" } + ) + frank_doe = Audiences::ExternalUser.create( + user_id: 321, + data: { displayName: "Frank Doe", territory: "Philadelphia" } + ) john_search = Audiences::UsersSearch.new(query: "John") phila_search = Audiences::UsersSearch.new(query: "Phila")