-
Notifications
You must be signed in to change notification settings - Fork 228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Problem with custom_columns and search #236
Comments
hey, try this one |
@ajahongir Thanks for the quick support, but still not working, now I get a: |
yep, now def filter_custom_column_condition |
@n-rodriguez can you help to figureout please? |
@ajahongir, @n-rodriguez still doesn't work... Now I get a |
Hi @ejmm320! Actually it looks like a bug from ajax-datatables-rails.
|
Hi @n-rodriguez !, thanks for the quickfix, however I'm still having problems with the filter_custom_column_condition method, and I think is related to the usage of aliased columns.
Ro::RoEventoPerdida.joins("left outer join ro_recuperaciones_eventos recup on
recup.ro_evento_perdida_id = ro_eventos_perdidas.id")
.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')
.where("ro_eventos_perdidas.monto_perdida > 0 and ro_eventos_perdidas.recuperable = true
and ro_eventos_perdidas.duplicado = false")
.group("ro_eventos_perdidas.id") And I don't know how to change the method in order to fix the search using this aliased fields, I'm able to get the value from the ActiveRecord::Relation using for example: Please could you give me a hint to fix this? Thanks again for your help! |
@ejmm320 can you put show your datatable? |
@ajahongir see this: https://gist.github.com/ejmm320/30817ab917f30c68147e126784b97d67, also I tried this way and it doesn't work: def filter_custom_column_condition
->(column) { ::Arel::Nodes::SqlLiteral.new(column.table[column.field]).eq(column.search.value) }
end |
@ejmm320 looks good, so whats is your latest error after those fixes? |
@ajahongir yes the problem was with the where clause and the aliased columns. The only way that I've found for now was forcing the filter method with raw SQL like this: def filter_custom_column_condition
->(column) { ::Arel::Nodes::SqlLiteral.new("CAST('#{column.field}' AS VARCHAR)").matches("#{ column.search.value }%") }
end Thanks for pointing me in the right direction, also thanks to @n-rodriguez for the fix related to this issue described before. |
@ejmm320 Do you still use the raw SQL solution? I have the same issue and this approach results in the search filtering on the name of my column instead of it's value. def filter_custom_column_condition
->(column, value) {::Arel::Nodes::SqlLiteral.new("CAST('purchase_value' AS VARCHAR)").matches("%#{column.search.value}%") }
end I wasn't sure it was actually looking at the column, so I just put some gibberish in as the column name expecting it to error when casting it but nothing: def filter_custom_column_condition
->(column, value) {::Arel::Nodes::SqlLiteral.new("CAST('asdfasdfasdfads' AS VARCHAR)").matches("%#{column.search.value}%") }
end I thought maybe it was just comparing the actual name of the column, 'purchase_value', so i did a search filtering on that string and all rows came up as a result so I think that is the case. I tried switching quotes and apostrophes within the new statement the original issue of UndefinedColumn error appears again. def filter_custom_column_condition
->(column, value) {::Arel::Nodes::SqlLiteral.new('CAST("purchase_value" AS VARCHAR)').matches("%#{ value }%") }
end @ajahongir, if I understand it correctly there are two queries made during the filter, a count and grabbing the data. It looks like the count query is not including the custom column for some reason. These are my resulting queries for the first code snippet. When I try the third, it errors out in the first query when it hits ...OR CAST("purchase_value'" AS VARCHAR)...
|
Hi there! Any news? |
I'm having a similar problem. The error is
users_datatable.rb
|
Casting worked for me: I had to change the proc to: |
While this solution doesn't raise an error, it doesn't seem to work as intended. This is comparing the string value of |
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:
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:
as you can see at the end of this query, it shows
OR 85 ILIKE '85%'
which is wrong, it should beOR 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
The text was updated successfully, but these errors were encountered: