bogdan / datagrid

Gem to create tables grids with sortable columns and filters
MIT License
1.02k stars 116 forks source link

Rails 5.1.0.rc1 with datagrid filter #215

Closed Bananaeyeball closed 7 years ago

Bananaeyeball commented 7 years ago

Hi Bogdan, hopefully you can review this issue soon. Those are my connected models:

 class Account < ApplicationRecord
has_many :invoices

class Invoice < ApplicationRecord
  belongs_to :account

filter(:account_name, :string, header: '...') do |value|
   self.joins(:account).where("accounts.name ilike '%#{value}%'")
  end

When I use this filter with rails 5.1, this exeption is risen:

PG::AmbiguousColumn: ERROR: column reference "id" is ambiguous LINE 1: ...."account_id" WHERE (name ilike '%...%') ORDER BY id DESC LI... ^ : SELECT 1 AS one FROM "acceptance_acts" INNER JOIN "accounts" ON "accounts"."id" = "acceptance_acts"."account_id" WHERE (name ilike '%...%') ORDER BY id DESC LIMIT $1 OFFSET $2

Thanks in advance, Looking forward to your answer!

bogdan commented 7 years ago

Where is ORDER BY id DESC coming from? I don't see anything that generates this code in your snippet. Find where does it come from and change id to acceptance_acts.id or whatever you mean there.

Bananaeyeball commented 7 years ago

ORDER BY id DESC comes to Grid filter from ActiveRecord. The most cunning is that on rails 5.0.2 the same request goes OK. But when I ONLY upgrade Rails gem to 5.1.0.rc1 filter starts to raise above exceptions. Can you look how it will behave itself with above model setup and rails 5.1? Thanks in advance!

bogdan commented 7 years ago

Why does it come from filter? You don't have any order defined in filter.

Bananaeyeball commented 7 years ago

Thank man, I found it! Everything works You can close this issue)