Error with paranoia gem
inyerade opened this issue · 3 comments
I am working with gems
- ajax-datatables-rails v0.4.0
- paranoia 2.2
Paranoia is for soft-delete, in my case I use in Tip. With this by default all query to Tip add a deleted_at IS NULL where clause
My datatable work perfect until implement paranoia, here is my code
class TipDatatable < AjaxDatatablesRails::Base
include ApplicationHelper
def view_columns
@view_columns ||= {
id: { source: "Tip.id", cond: :eq },
name: { source: "Tip.name", cond: :like },
description: { source: "Tip.description", cond: :like },
tip_level: { source: "TipLevel.name", cond: :like },
start_at: { source: "Tip.start_at" },
display_mode: { source: "DisplayMode.name" },
end_at: { source: "Tip.end_at" }
}
end
def data
records.map do |record|
{
id: ( link_to record.id , tip_path( record ) ).to_s,
name: record.name,
description: record.description,
tip_level: record.tip_level.try(:name),
display_mode: record.display_mode.try(:name),
start_at: ( ldate record.start_at , format: :gringo ).to_s,
end_at: ( ldate record.end_at , format: :gringo ).to_s
}
end
end
private
def_delegators :@view, :link_to, :h, :mailto, :edit_tip_path, :tip_path
def get_raw_records
Tip.joins( :tip_level )
end
end
Debugging I found the problem was the agrupation of AS foo and the paranoia where clause deleted_at IS NULL. here is the problem query:
SELECT COUNT(*) FROM (SELECT "tips".* FROM "tips" INNER JOIN "tip_levels" ON "tip_levels"."id" = "tips"."tip_level_id" WHERE "tips"."deleted_at" IS NULL) AS foo WHERE "tips"."deleted_at" IS NULL)
My workaround was disable the default deleted_at IS NULL and change the get_raw_records function like this
def get_raw_records
Tip.without_deleted.joins( :tip_level )
end
The problem is I have to change all part where I query Tip and add without_deleted.
My question is: is there a solution? I think if exist an easy way of overwrite datatable count method I can solve my problem
(sorry my poor english)
https://github.com/jbox-web/ajax-datatables-rails/blob/600c0de52c7f9f5c093d18a9532ae80e1d0f2456/lib/ajax-datatables-rails/base.rb#L40/
you can override this method in your datatable
thanks @ajahongir
I use:
...
def as_json(*)
{
recordsTotal: get_raw_records.count,
recordsFiltered: filter_records(get_raw_records).count,
data: data
}
end
private
def_delegators :@view, :link_to, :h, :mailto, :edit_tip_path, :tip_path
def get_raw_records
Tip.joins( :tip_level , :display_mode )
end
...
and work perfect. Thanks
This is weird because the as foo thing was removed when introducing Oracle tests : 600c0de#diff-f80122f94271d4631a69ddbbdf2218c6L98