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
custom sort on custom column in index ? #3085
Comments
What database queries does |
It joins tables, mostly. Something like : Address.joins('LEFT JOIN vehicles ON vehicles.id = addresses.addressable_id').
joins('LEFT JOIN users ON users.id = vehicles.main_owner_id').
where('(point(addresses.longitude, addresses.latitude) <@> point(?,?)) <= ? AND addressable_type = ?',
self.longitude, self.latitude, radius * 0.621371192, 'Vehicle').
where('users.owner_completion >= ?', completion) |
Ideally you'd be able to pass raw SQL like this: column 'Drivers', sortable: 'sum(vehicles.id)' but that's not currently possible. |
Yeah, it'd be perfect if I could to that! So it means that we currently cannot sort a custom column? There parameter must be an attribute and cannot be a method which do the sort? Thanks |
I've made some success locally, but haven't add it as a pull-request yet. |
Cool @dmitry ! Could you put it in a gist or tell me how to do this? It'd be great! |
@dmitry Could you help me on that? |
@Epiju look to the |
related to #3173 |
Ultimately there's no regular expression that could match every possible query. We're going to have to remove the validation entirely. |
I thing we could have a sql injection problem then! We should rewrite the sorting. column :foo, sorting: 'bar.foo' do; end In that case we give We should pass only the column name and sort order through the url and make a lookup to generate the order query. |
👍 |
+1 @timoschilling , wanting to sort by count(...) but running into this wall. |
@gkop depend on what kind of |
Thanks @timoschilling , you're referring to a counter cache eh? My scenario is habtm with conditions. I don't think you can use the counter cache with conditions - it gets messy - IE a column of an associated row may change, which should affect our count, so then activerecord must not only observe the lifecycle of the joiner rows but also the associated records. |
@gkop yes, I think it don't work for your case too. But I many "I want to sort by count" cases it helps |
I also wanna do something like: column :foo, sortable: 'lower(foo)' for case-insensitive sorting! |
guys, how you can override sort of
and then describe column like
|
Just an Idea, would be nice to have something like ActiveAdmin.register User do
config.order_clause_class = ActiveAdmin::UserOrderClause
end in this case we can extend |
What is the status of this issue? I am coming across the same problem as the original poster. Is there a workaround? |
@smtheard I gave up without discovering a workaround. Instead I have a numeric filter on the counts, which required custom scopes. |
We could use existing aliases: controller do
def scoped_collection
super.includes(:authors).select '*, count(authors.*) as author_count'
end
end
index do
column(:authors, sortable: 'author_count') { |b| b.authors.count }
end Or we could allow controller do
def scoped_collection
super.includes(:authors)
end
end
index do
column(:authors, sortable: 'count(authors.*) as author_count') { |b| b.authors.count }
end In both cases, we'd use the alias in the HTML, instead of the SQL fragment itself. |
I'd probably prefer to only support one of these, to reduce complexity. What does everyone think? |
The first option would better suit @Epiju's needs. It could also allow you to do this: controller do
def scoped_collection
super.includes(:authors).select '*, count(authors.*) as author_count'
end
end
index do
column :author_count, sortable: true
end |
Guys and @seanlinsley, i tried to implement the following approach but facing a issue in it. Here goes the code snippet:
I get the following error: It would be really helpful if someone support me in resolving this issue! |
You need |
Thanks @dmitry i tried with that, then i faced the error:
So changed my code to:
Any idea how to proceed further? |
I have tried @seanlinsley solution but this is not working as include doesn't include any has_many relation column. I have come up with new solution here
|
Cool, thank u so much @krishnasrihari, i have analyzed it and would like to suggest a minor change to resolve sorting URL issue., Finally, here goes the working solution to get revealed so that it'll be helpful for others too.,
In my case, the condition is needed :) However the general solution will be.,
|
Thanks; this solved my problem.
|
@nicodeceulaer Thanks for the heads up! missed it while editing. Updated it now ;) |
The above solution does not work very well with the pagination logic. def scoped_collection
super.joins('LEFT JOIN "debugdumps" ON "debugdumps"."crash_id" = "crashes"."id"')
.select('crashes.*, COUNT(debugdumps.id) as debugdump_count')
.group('crashes.id')
end SELECT COUNT(*) AS count_all, crashes.id AS crashes_id FROM "crashes" LEFT JOIN "debugdumps" ON "debugdumps"."crash_id" = "crashes"."id" GROUP BY crashes.id Regretfully, the same scoped collection also blocks against the current pagination disable code. So the only way out now is to make my own pagination logic as proposed in stackoverflow |
test and feedback are appriciated |
Is there a possibility to sort in index by method (non-db backed values)? |
This is the way I'm sorting by custom values and preventing n + 1 queries
and then in your tech_results section, you want to render an organization field, in my case the value
|
How do I sort store attribute accessor and method That is, I need my table to be sortable by :status, :another_email_id, :another_phone_number, average user.rb
admin/users.rb
|
Consider sorting that requires a `JOIN` as part of the SQL query (or really anything that was raised in activeadmin#3085): ```ruby ActiveAdmin.register Foo do config.order_clause = FooOrderClause index do column :latest_bar, sortable: 'bars.created_at' do ... end end end class FooOrderClause < ActiveAdmin::OrderClause def apply(chain) case table_column when 'bars.created_at' chain.joins(:bars).select(table_column) else super end end end ``` Oftentimes, including that `JOIN` is not a huge deal in terms of performance, however, there have been situations where that table/column can increase the time it takes for a query to run by a couple hundred milliseconds, for various reasons. Increasing the time to load the page slightly (0.1s) is not a big deal, in an admin, but imagine if you had 7-10 different scopes on that page as well? Because scopes leverage `collection_before_scope`, they will proceed to include the `JOIN` in their query, even though it is only necessary to do sorting of a collection that is not actually being sorted, only counted. Applying sorting of the collection *after* scoping, instead of before, should have no bearing on the end-result, while ensuring that any custom sorting logic is not needlessly applied to all scope count queries.
Hi,
I'm looking for a way to make a custom sort on a custom column in an index page.
My User#drivers_in_neighborhood method is in User model and is doing SQL queries.
I just want to make the column "Drivers" sortable.
How to do that?
Thanks!
The text was updated successfully, but these errors were encountered: