Skip to content

Error with paranoia gem #223

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
inyerade opened this issue May 24, 2017 · 3 comments
Closed

Error with paranoia gem #223

inyerade opened this issue May 24, 2017 · 3 comments

Comments

@inyerade
Copy link

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)

@ajahongir
Copy link
Collaborator

@inyerade
Copy link
Author

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

@n-rodriguez
Copy link
Member

This is weird because the as foo thing was removed when introducing Oracle tests : 600c0de#diff-f80122f94271d4631a69ddbbdf2218c6L98

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

No branches or pull requests

3 participants