Skip to content
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

searching on association table (not foreign key) causes crashes #153

Closed
nekapoor opened this issue Apr 8, 2021 · 1 comment
Closed

searching on association table (not foreign key) causes crashes #153

nekapoor opened this issue Apr 8, 2021 · 1 comment

Comments

@nekapoor
Copy link

nekapoor commented Apr 8, 2021

We have and parents table and a students table (A parent has many students). There's a parent_id field on the students table.

On the parents table, there is also a separate uid field which is a longer unique field.

ANd in the students datatable, I display the parent uid field with:

    col :parent_uid, label: 'Parent uid', do |student|
      student.parent.uid
    end

but when I search in this column, the app will crash often. The query is something like select * from parents where id in <all the ids>. You can see an image for what it looks like.

Screen Shot 2021-04-08 at 11 48 01 AM

I'm not sure if I've explained this well, but happy to clarify anything. Any thoughts on this?

Thank you!

@matt-riemer
Copy link
Member

Hey there! :)

Yep, looking at the screenshot here, I think the problem is actually with sorting by the parent.uid column. The effective_datatables tries to sort without doing any internal joins or changing your collection at all.

The quick fix is to just to disable sorting, by going col(:parent_uid, sort: false)

A second fix, would be to use the join syntax. This would change your collection to be:

collection do
  Student.all.joins(:parent)
end

and then define your column:

col('parent.uid')

And it'll sort with a much better SQL query.

If you don't want to change the whole collection, you could only perform this join when sorting on this column, something like:

col(:parent_uid) do |student|
  student.parent.uid
end.sort do |collection, direction, column, sql_column|
    collection.joins(:parent).order(:parent => :uid, direction)
end

Hope this actually helps! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants