Description
Hi,
I am facing in writing the joins or includes complex queries for this gem.
Models:-
class Contact < ApplicationRecord
has_many :contact_contact_categories
has_many :contact_categories, through: :contact_contact_categories
end
class ContactCategory < ApplicationRecord
has_many :contact_contact_categories, dependent: :destroy
has_many :contacts, through: :contact_contact_categories
end
This is my contact_datatable.rb
class ContactDatatable < AjaxDatatablesRails::ActiveRecord
extend Forwardable
def_delegators :@view, :link_to, :edit_contact_path
def initialize(params, opts = {})
@view = opts[:view_context]
super
end
def view_columns
@view_columns ||= {
name: { source: "Contact.decorate.name" },
organization_name: { source: "Contact.decorate.organization_name" },
contact_categories: { source: "Contact.decorate.contact_categories" },
phone: { source: "Contact.decorate.phone" },
email: { source: "Contact.decorate.email" },
dt_actions: { source: "Contact.decorate.dt_actions", searchable: false, orderable: false },
}
end
def data
records.map do |record|
decorated_record = record.decorate
{
name: decorated_record.name,
organization_name: decorated_record.organization_name,
contact_categories: decorated_record.contact_categories,
phone: decorated_record.phone,
email: decorated_record.email,
dt_actions: decorated_record.dt_actions,
DT_RowId: record.id,
}
end
end
def get_raw_records
params[:contacts].joins(:contact_contact_categories)
end
end
contact_decorator.rb
class ContactDecorator < Draper::Decorator
include Draper::LazyHelpers
delegate_all
def dt_actions
"
end
def name
link_to(object.name, edit_contact_path(object), class: 'custom-href', data: { turbolinks: false })
end
def email
link_to(object.email.presence || '', edit_contact_path(object), class: 'custom-href', data: { turbolinks: false })
end
def organization_name
link_to(object.organization_name.presence || '', edit_contact_path(object), class: 'custom-href', data: { turbolinks: false })
end
def phone
link_to(object.phone_number_1.presence || '', edit_contact_path(object), class: 'custom-href', data: { turbolinks: false })
end
def contact_categories
link_to(object.contact_categories.pluck(:name).join(', ') || '', edit_contact_path(object), class: 'custom-href', data: { turbolinks: false })
end
end
This is the view
NAME | ORGANIZATION | CATEGORIES | PHONE |
---|
<%= render partial: "/shared/confirmation_popup", locals: { record_type: 'Contact' } %>
<script> jQuery(document).ready(function() { $('#contacts-datatable').dataTable({ destroy: true, order: [0, 'ASC'], dom: getDomContent(true, ''), iDisplayLength: 25, pagingType: 'simple_numbers', language: { lengthMenu: "Per Page: _MENU_ ", paginate: { next: '', previous: '' } }, aoColumnDefs: [{ bSortable: false, aTargets: [-1] }], drawCallback: function(settings) { var pagination = $(this).closest('.dataTables_wrapper').find('.dataTables_paginate'); pagination.toggle(this.api().page.info().pages > 1); }, processing: true, serverSide: true, ajax: { url: $('#contacts-datatable').data('source') }, columns: [ {data: "name"}, {data: "organization_name"}, {data: "contact_categories"}, {data: "phone"}, {data: "email"}, {data: "dt_actions"} ] }); $('.footable').footable(); }); </script>When i am trying to sort with the column categories, it is giving errors.
n ActiveRecord::StatementInvalid occurred in contacts#index:
PG::UndefinedColumn: ERROR: column contacts.contact_categories does not exist
LINE 1: ...LL AND "contacts"."organization_id" = $1 ORDER BY contacts.c...
^
: SELECT "contacts".* FROM "contacts" INNER JOIN "contact_contact_categories" ON "contact_contact_categories"."contact_id" = "contacts"."id" WHERE "contacts"."deleted_at" IS NULL AND "contacts"."organization_id" = $1 ORDER BY contacts.contact_categories ASC LIMIT $2 OFFSET $3
How will i fix it?