ajax-datatables-rails icon indicating copy to clipboard operation
ajax-datatables-rails copied to clipboard

Sorting Based on one Column for Many Many Relationships

Open vgupta1809 opened this issue 5 years ago • 0 comments
trafficstars

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 "

".html_safe 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 EMAIL

<%= render partial: "/shared/confirmation_popup", locals: { record_type: 'Contact' } %>

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?

vgupta1809 avatar Jan 29 '20 11:01 vgupta1809