-
Notifications
You must be signed in to change notification settings - Fork 228
Perform searching & sorting on serialized hash column #140
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
Comments
@tejashande put your example here |
Suppose it is
I tried to perform search & sort on first_name,middle_name & last_name by
But this didn't worked. Is there any option to perform search & sort on specified fields? Thanks for the prompt reply. |
I recomend you use latest version of this gem. and you can follow this example - https://github.com/ajahongir/ajax-datatables-rails-v-0-4-0-how-to/ |
@ajahongir By looking at your city_datatable.rb, it doesn't seem like city model contain serialized hash. If possible, will you please provide the sample for the same as I couldn't find the way to implement in my case. |
@tejashande are you trying to sort order by nested hash? |
@ajahongir Yes. I was trying to search or sort column by nested hash. |
can you please put your datatable defining on js? |
@ajahongir Here is the code
|
|
|
I am not sure but it can be usefull - try to define columns separately columns: [
data: "id"
,
data: "Student.first_name"
,
data: "Student.last_name"
....
] |
@ajahongir Is there any example which will show to define columns separately? By the way, thanks for prompt replies. |
That didn't worked. |
in you model you are saving name as Hash? what is you sql query to sorting? |
@ajahongir Even I was thinking the same that what could be the sql query to retrieve the records from database. I thought about view but it again put me in trouble. |
I gues you are using PG as database. |
All the way I was thinking the same, but I thought that there might be something which would make my task more easy. |
Hello, Does any one know how to use ORM as I was unable to implement search & sort functionality on nested hash columns? |
@tejashande you can try to build sql query first then implement it in ORM. |
Hey @ajahongir I came up with the sql query. Now I am using the json datatype for the name column & I am using mysql database. & mysql query would be select json_extract(name, '$.first_name') from students order by json_extract(name, '$.first_name') desc; Now Is there anyway to perform search & sort on this columns? |
@tejashande let me made some changes on the gem. |
Hey @ajahongir I came up with solution for json datatype I made few changes in new_search_condition method of Base class & that did the trick. Find the changes at https://www.diffchecker.com/gx2fdyzf Existing Method (ajax-datatables-rails version - 0.3.1). def new_search_condition(column, value)
model, column = column.split('.')
model = model.constantize
casted_column = ::Arel::Nodes::NamedFunction.new('CAST', [model.arel_table[column.to_sym].as(typecast)])
casted_column.matches("%#{value}%")
end New Method def new_search_condition(column, value)
model, column, attribute = column.split('.')
model = model.constantize
if model.columns_hash[column].sql_type == 'json'
Arel.sql("JSON_EXTRACT(#{column}, '$.#{attribute}')")
else
::Arel::Nodes::NamedFunction.new(
'CAST', [model.arel_table[column.to_sym].as(typecast)]
)
end.matches("%#{value}%")
end I want to do the same thing with clear approach using Arel. Could you please suggest the way to implement the same with the help of Arel? I saw that there were few changes in new_search_condition method at master branch. Thanks! |
@tejashande sorry too late answering.
Iam not sure about this changes. you can use latest version on my fork(its not accepted yet But I hope soonly will be accepted) in this version you can override column searching(look at here https://github.com/ajahongir/ajax-datatables-rails/blob/filtering-improvements/lib/ajax-datatables-rails/datatable/column.rb#L80) as shown on sample project(https://github.com/ajahongir/ajax-datatables-rails-v-0-4-0-how-to/blob/master/app/datatables/city_datatable.rb#L9) in your case it will looks something like: class StudentDatatable < AjaxDatatablesRails::Base
def view_columns
@view_columns ||= {
id: { source: "Student.id", cond: :eq },
first_name: { source: "Student.name", cond: fist_name},
...
}
end
private
def data
records.map do |city|
{
id: student.id,
first_name: student.first_name,
....
}
end
end
def get_raw_records
Student.all
end
def first_name
->(column) { Arel.sql("JSON_EXTRACT(#{column.field}, '$.first_name')") }
end
#or
#def name(attribute)
# ->(column) { Arel.sql("JSON_EXTRACT(#{column.field}, '$.#{attribute}')") }
#end
end |
or try something this: def first_name
->(column) { Arel::Nodes::NamedFunction('JSON_EXTRACT', [column.field, "#{$.first_name}"] }
end |
@ajahongir I have similar case where I'd like to filter by values in JSONB column
At the moment when I try to filter How should I tweak my |
which version are you using now? |
@ajahongir I'm using latest version (0.4.0.). |
for some reasons passing |
@ajahongir Do you think there is some workaround? Is there anything that can be done in my case? |
Can you please try with the master branch of the repo? |
A new issue is opened : #277 |
I have serialized hash column but I am unable to perform searching or sorting on those columns. Is there a way to perform searching & sorting on the hash of serialized column?
Any help will be greatly appreciated.
The text was updated successfully, but these errors were encountered: