ajax-datatables-rails: Problem with custom_columns and search
I have a problem regarding the searchable functionality of one of my datatables because I added a couple of custom columns in my query (both sum calculations).
First, I had to override the method as_json in the way described here: #159 in order to display the table properly (count and number of rows), but now I’m unable to perform any search.
My first attempt was to override using a custom filter method following this example, I added this code:
def filter_custom_column_condition
->(column) { ::Arel::Nodes::SqlLiteral.new(column.field.to_s).matches("#{ column.search.value }%") }
end
without luck, both columns are Numeric, I also tried using the cond: :eq as suggested in docs but another error appears.
Checking logs I realized that it looks like the query doesn’t consider the custom columns because the name (an alias) is not present. To give an example I put on the search box the number “85” in order to show the generated sql:
SELECT COUNT(*) FROM (SELECT ro_eventos_perdidas.id, ro_eventos_perdidas.fecha_ocurrencia,
ro_eventos_perdidas.descripcion, ro_eventos_perdidas.recuperable, ro_eventos_perdidas.monto_perdida,
coalesce(sum(monto_recuperado),0) as monto_recuperado,
coalesce(ro_eventos_perdidas.monto_perdida - coalesce(sum(monto_recuperado),0),0) as monto_pendiente FROM "ro_eventos_perdidas" left outer join ro_recuperaciones_eventos recup on
recup.ro_evento_perdida_id = ro_eventos_perdidas.id WHERE (ro_eventos_perdidas.monto_perdida > 0 and ro_eventos_perdidas.recuperable = true
and ro_eventos_perdidas.duplicado = false ) AND ((((((CAST("ro_eventos_perdidas"."id" AS VARCHAR) ILIKE '%85%' OR CAST("ro_eventos_perdidas"."fecha_ocurrencia" AS VARCHAR) ILIKE '%85%') OR CAST("ro_eventos_perdidas"."descripcion" AS VARCHAR) ILIKE '%85%') OR CAST("ro_eventos_perdidas"."recuperable" AS VARCHAR) ILIKE '%85%') OR CAST("ro_eventos_perdidas"."monto_perdida" AS VARCHAR) ILIKE '%85%') OR 85 ILIKE '85%') OR 85 ILIKE '85%') GROUP BY ro_eventos_perdidas.id) AS foo
as you can see at the end of this query, it shows OR 85 ILIKE '85%' which is wrong, it should be OR CAST("ro_eventos_perdidas"."monto_recuperado" AS VARCHAR) ILIKE '%85%')… which is one of my custom columns.
I’m not sure if I have to add something else or maybe something is missing, so please point me in the right direction.
Regards.
Stack:
Postgresql 9.4 Rails 4.2.7 jquery-datatables-rails 3.3.0 ajax-datatables-rails 0.4.0
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 16 (2 by maintainers)
Hi @ejmm320! Actually it looks like a bug from ajax-datatables-rails. It was introduced by https://github.com/jbox-web/ajax-datatables-rails/commit/92c2254208c78fe1bf60df552d62171aea3834b6#diff-a7f0f3ee893084549cc5255576b14fe3L38. At the time passing a value to the
filtermethod without using it was a bit weird to me. Now I better understand why we passselfto the Proc. This part is not covered by a test, do you have a use case to write one? It’s now fixed : https://github.com/jbox-web/ajax-datatables-rails/commit/07795fd26849ff1b3b567f4ce967f722907a45be For now you can test it by changing your Gemfile :