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

Custom search #50

Closed
Nerian opened this issue Jun 12, 2017 · 6 comments
Closed

Custom search #50

Nerian opened this issue Jun 12, 2017 · 6 comments

Comments

@Nerian
Copy link
Contributor

Nerian commented Jun 12, 2017

Hi,

I am trying to implement a custom search method, but the collection I am getting in the search method is an array instead of a scope.

col :member_id, sort: false, label: 'Member', search: { as: :select, collection: members_collection } do |charge|
  link_to charge.member.name, manager_accounting.member_path(current_space, charge.member)
end.search do |collection, term, column, sql_column|
  collection.joins(:invoice).where(invoices: { member_id: term })
end

collection do
  charges = current_space.charges.includes(:payment_method, :invoice, invoice: :member)
end

The collection in the search method holds an array of arrays. But what should be coming out of the collection block is a scope.

This is the type of the charges variable in the collection block:

[2] pry(#<Effective::DatatableDslTool>)> charges.class
=> Charge::ActiveRecord_AssociationRelation

What am I doing wrong?

@matt-riemer
Copy link
Member

I would expect the collection there to be an ActiveRecord::Relation there. Can I see the whole datatable? Could be something weird going on.

@Nerian
Copy link
Contributor Author

Nerian commented Jun 12, 2017

@matt-riemer
Copy link
Member

matt-riemer commented Jun 13, 2017

I think datatables is treating member_id as an array-based column, because Charge.new doesn't respond to member_id.

Try adding sql_column to the col line:

col :member_id, sql_column: 'invoices.member_id'

You might not even need a custom search method anymore!

@Nerian
Copy link
Contributor Author

Nerian commented Jun 13, 2017

Nice :)

That fixes it. Should we consider this a bug or should I be adding this parameter in other places too?

@matt-riemer
Copy link
Member

I don't consider this a bug.

Datatables is pretty smart about a single model (with it's belongs_to and has_many) but when you're doing crazy joins and such it needs a little bit of help :)

I will add to the documentation about this!

Thanks as always @Nerian

@Nerian
Copy link
Contributor Author

Nerian commented Jun 13, 2017

Sounds good :) Thanks for your work. The new version is working quite well :)

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